In my application I'm creating a PDF reader. To cater for performance, I draw a view at default scale. This view is not CATiledLayer based. When user zooms in a bit, I overlay this view with a CATiledLayer one which gives much crisper image and low memory usage in normal use cases.
Now the problem is that in default state (when CALayer view is being shown) the image is coming out a bit blurry. When I try to zoom in just a wee bit and CATiledLayer kicks in, the same text and everything becomes sharp.
Here's the code for the CATiledLayer view (which shows crisp image and text as it should be):
- (void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)context{
CGPDFPageRef drawPDFPageRef = NULL;
CGPDFDocumentRef drawPDFDocRef = NULL;
@synchronized(self) {
if (_PDFDocRef==nil) {
NSLog(@"PDF NIL");
_PDFDocRef = [SharedPDFDocRef sharedInstanceForUrl:_fileURL andPhrase:_password];
_PDFPageRef = CGPDFDocumentGetPage(_PDFDocRef, _page);
}
drawPDFDocRef = _PDFDocRef;
drawPDFPageRef = _PDFPageRef;
}
if (drawPDFPageRef != NULL) {
CGContextTranslateCTM(context, 0.0f, self.bounds.size.height);
CGContextScaleCTM(context, 1.0f, -1.0f);
CGAffineTransform transform =
CGPDFPageGetDrawingTransform(drawPDFPageRef, kCGPDFCropBox, layer.bounds, 0, true);
CGContextConcatCTM(context, transform);
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
CGContextSetInterpolationQuality(context, kCGInterpolationDefault);
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextFillRect(context, CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFBleedBox));
CGContextDrawPDFPage(context, drawPDFPageRef);
}
//CGPDFPageRelease(drawPDFPageRef); CGPDFDocumentRelease(drawPDFDocRef);
}
Here's the code for the Normal view (shows okay but image is not in the same quality as CATiledLayer one):
- (void) drawRect:(CGRect)rect {
[super drawRect:rect];
CGPDFPageRef drawPDFPageRef = NULL;
CGPDFDocumentRef drawPDFDocRef = NULL;
@synchronized(self) {
drawPDFDocRef = CGPDFDocumentRetain(_PDFDocRef);
drawPDFPageRef = CGPDFPageRetain(_PDFPageRef);
}
// get the page reference
CGPDFPageRef page = drawPDFPageRef;
CGContextRef context = UIGraphicsGetCurrentContext();
// white background
// transformation/-lation
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.00f, -1.00f);
CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page,
kCGPDFCropBox, self.bounds, 0, true));
CGContextSetInterpolationQuality(context, kCGInterpolationDefault);
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextFillRect(context, CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFBleedBox));
CGContextDrawPDFPage(context, page);
CGPDFPageRelease(drawPDFPageRef);
CGPDFDocumentRelease(drawPDFDocRef);
}