Task: create an image from a PDF file as a quick preview for my PDF file displayed with CATiledLayer(slower, higher resolution). Problem: I get a rather quick error warning on my iPad, “Memory Alert: Level = 1” and shortly after “Received Memory Alert. Level = 2” .. then the application crashes.
- (void) drawSinglePage:(CALayer *)layer inContext:(CGContextRef)ctx {
CGContextSaveGState(ctx);
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
@synchronized(self) {
CGPDFPageRef page = CGPDFDocumentGetPage(page1, 1);
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
CGFloat scaleRatio = 960/pageRect.size.height;
CGFloat yOffset = ((960-pageRect.size.height)/scaleRatio)+960;
CGContextTranslateCTM(ctx, -(layer.bounds.size.width-pageRect.size.width)/2, yOffset);
CGContextScaleCTM(ctx, scaleRatio, -scaleRatio);
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, layer.bounds, 0, true));
CGContextDrawPDFPage(ctx, page);
UIGraphicsBeginImageContext(layer.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGRect prob = CGContextGetClipBoundingBox(context);
CGContextFillRect(context, prob);
CGContextSaveGState(context);
CGContextSetInterpolationQuality(context, kCGInterpolationLow);
CGContextTranslateCTM(context, -(layer.bounds.size.width-pageRect.size.width)/2, yOffset);
CGContextScaleCTM(context, scaleRatio, -scaleRatio);
CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, CGRectMake(0.0, 0.0, layer.bounds.size.width, layer.bounds.size.height), 0, true));
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
UIImage *back = UIGraphicsGetImageFromCurrentImageContext();
NSData *jpegDataFile = UIImageJPEGRepresentation(back, 0.2f);
UIImage *smBack = [[UIImage alloc] initWithData:jpegDataFile];
UIGraphicsEndImageContext();
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:smBack];
backgroundImageView.frame = CGRectMake(0.0, 0.0, smBack.size.width, smBack.size.height);
backgroundImageView.contentMode = UIViewContentModeTopLeft;
backgroundImageView.opaque = YES;
[self.view addSubview:backgroundImageView];
[self.view sendSubviewToBack:backgroundImageView];
[backgroundImageView release];
}
CGContextRestoreGState(ctx);
}
}
: , - , - , . .
: "CGPDFDocumentRelease(page1)" - (void)dealloc ViewController.