Memory Leak when create a UIImage - ios

I have following method to take a pdf file in documents directory and create a thumbnail out of it. This method is leaking memory in two places as shown in comments. Since I am using ARC I am not sure why is it leaking memory. How can I solve this.
+ (UIImage*)createPdfThumbnail:(NSString*)pdfFilePath {
NSURL *targetURL = [NSURL fileURLWithPath:pdfFilePath];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)targetURL); // 3.0% of memory leak
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);//for the first page
CGRect aRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
UIGraphicsBeginImageContext(aRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, aRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, -(aRect.origin.x), -(aRect.origin.y));
CGContextSetGrayFillColor(context, 1.0, 1.0);
CGContextFillRect(context, aRect);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, aRect, 0, false);
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, page);
UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext(); // 97% of memory leak
CGContextRestoreGState(context);
UIGraphicsEndImageContext();
CGPDFDocumentRelease(pdf);
return thumbnail;
}
EDIT:
-(void)fromJSON:(NSDictionary *)JSON{
[super fromJSON:JSON];
self.path = JSON[#"path"];
//Create and save thumbnail
if (self.parentSpecSheet != nil){
#autoreleasepool {
UIImage* thumbnail = [Utilities createPdfThumbnail:self.path];
Photo* thumbnailPhoto = [Photo addObject];
[thumbnailPhoto setDelta:#(0)];
[thumbnailPhoto setImage:thumbnail];
[thumbnailPhoto.file setDelta:#(0)];
self.parentSpecSheet.thumbnail = thumbnailPhoto;
}
}
}

Two thoughts:
I experience a significant leak from CGContextDrawPDFPage when I test your code in iOS5 (and if you search for "CGContextDrawPDFPage leak", you'll see tons of references to permutations of this problem). This appears to be a known problem.
I see no appreciable leak in iOS 6 from the above code, though.
If you're still seeing this leak in iOS 6, then I suspect the problem does not rest in the above code. Do you have any other leaks reported? I'd also suggest you confirm that the object that owns this thumbnail is successfully getting deallocated itself (e.g. log/breakpoint in its dealloc method).
Unfortunately, when you look at the leaks tool, it's reporting where the leaked object was instantiated, not where the leak took place. You might want to confirm that the owner of this thumbnail is not, somehow, maintaining a strong reference to it (e.g., the owner, itself, has a retain cycle, or something like that).

Related

CGContextDrawImage off Screen performance

I have been using the following code to create an image off the screen (creating a
context, off-screen in background thread using GDC ) and later render it in the
main thread on the screen only when i receive updated "array of UIImages"
.
I found by doing so the rending path , lines .etc was much faster then dowing it the
drawRect: method of the UIView .
The issue that I'm facing now is that when trying to rending UIImage class using
CGContextDrawImage() it get really slow over ~1.4-2.5 sec to render. From searching
the main issue was that it spends most of its time deflating the png file. I have
tried infating the png image and use CGImageRef still long rending time. I dont have
a control on the data array that contain list if png files it comes in.
Would breaking the image to small pieces or and, using CALayer help , how i can stitch
those images without using "CGContextDrawImage" is it possible ?
I'm not sure what is best way to fix this issues and how , any Idea ?
Thanks In Advance
void creatImageInBackground
{
size = CGSizeMake (self.frame.size.width,self.frame.size.height);
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
UIGraphicsPushContext(context);
for(UIImage * img in imgList )
{
ulXY = <calcuate >;
lrXY = <calcuate >;
imRec = CGRectMake( ulXY.x,-ulXY.y,(lrXY.x-ulXY.x) ,(lrXY.y -ulXY.y) );
CGContextTranslateCTM( context, 0,(lrXY.y-ulXY.y) );
CGContextScaleCTM(context, 1, -1);
CGContextDrawImage(context, imRec,[[img image] CGImage]);
//[img drawInRect:imRec];
}
UIGraphicsPopContext();
[outputImage release];
outputImage = UIGraphicsGetImageFromCurrentImageContext() ;
[outputImage retain];
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^(void)
{
[self setNeedsDisplay];
[self setHidden:NO];
});
}
Then
- (void)drawRect:(CGRect)rect
{
CGPoint imagePoint = CGPointMake(0, 0);
[outputImage drawAtPoint:imagePoint];
}

Core Graphics memory leaks while displaying pdf images

I have a subclass of UIImageView view that loads pdf image data, so that I can have a resolution independent graphic in my view. Works well for the stated purpose, but I am getting memory leaks with this, according to an instruments leaks profile.
Here is the code below that I believe should be responsible for the leaks. I am trying to track down the problem, but I am a little foggy on how to pinpoint the issue.
- (id)initWithPDFResourceAtPath:(NSString *)path center:(CGPoint)center {
if ((self = [super init])){
CGPDFPageRelease(pageRef);
CGPDFDocumentRef documentRef = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:path]);
pageRef = CGPDFDocumentGetPage(documentRef, 1);
CGPDFPageRetain(pageRef);
CGPDFDocumentRelease(documentRef);
[self setBounds];
}
return self;
}
-(void)setBounds {
[self setBounds:CGRectApplyAffineTransform(CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox), CGAffineTransformMakeScale(scaleH, scaleV))];
size = self.bounds.size;
[self getPDFimage];
}
-(void)getPDFimage {
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, scaleH, scaleV);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
CGContextDrawPDFPage(context, pageRef);
[self setImage:UIGraphicsGetImageFromCurrentImageContext()];
}
You forgot to call UIGraphicsEndImageContext(). Change your code to:
UIImage *image = [self setImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
return image;
EDIT1: your code has this pageRef variable - is it an ivar or a static? If an ivar you better release it with CGPDFPageRelease() in the dealloc method. [It really should be an ivar]
EDIT2: See attached screen shot on Object Alloc. You can see the type and current amount and its ordered from most to least.
EDIT3: all else fails create a demo project that has the same problem and post it on Dropbox.
EDIT4: Code was uploaded to: here (I cannot look at it til May 28th)
EDIT5: The problem is that pageRef is not ever released. So:
1) remove this from your init method, as it does nothing:
CGPDFPageRelease(pageRef);
2 and move it to a new dealloc method:
- (void)dealloc
{
CGPDFPageRelease(pageRef);
}

iOS leak instrument CGContextDrawPDFPage

I know this question has been asked several times, but I couldn't solve it for my particular case. CGContextDrawPDFPage is indicated as a leak in the leak instrument. Also when this segment of code is run the app crashes which I'm really sure is due to memory issues.
pdfURLDocument = [[NSURL alloc] initFileURLWithPath: [docsDir stringByAppendingPathComponent:documentName]];
pdfDocument = CGPDFDocumentCreateWithURL((CFURLRef)pdfURLDocument);
[pdfURLDocument release];
page = CGPDFDocumentGetPage(pdfDocument, 1);
CGPDFPageRetain(page);
// determine the size of the PDF page
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
pdfScaleWidth = (1/((CGFloat) gridSizeDocument)) * self.frame.size.width/pageRect.size.width;
pdfScaleHeight = (1/((CGFloat) gridSizeDocument)) * self.frame.size.height/pageRect.size.height;
pageRect.size = CGSizeMake(pageRect.size.width*pdfScaleWidth, pageRect.size.height*pdfScaleHeight);
// Create a low res image representation of the PDF page
UIGraphicsBeginImageContext(pageRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// First fill the background with white.
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context,pageRect);
CGContextSaveGState(context);
// Flip the context so that the PDF page is rendered
// right side up.
CGContextTranslateCTM(context, 0.0, pageRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Scale the context so that the PDF page is rendered
// at the correct size for the zoom level.
CGContextScaleCTM(context, pdfScaleWidth, pdfScaleHeight);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGPDFPageRelease(page);
Also, I included CGPDFPageRelease(page); in the dealloc method. Also, it might be helpful to add that it works fine for small documents, but only crashes on large ones. Memory leaks still persist in the smaller ones, however.
I know this is an old question, but two observations:
You need to release your pdfDocument:
CGPDFDocumentRelease(pdfDocument);
You should not release the page with CGPDFPageRelease(page), though, because that is an autoreleased object and you don't own it (unless, of course, you retained it with CGPDFPageRetain).
If you use the static analyzer ("Analyze" on Xcode's "Product" menu), it should point out both of those issues.
The fundamental problem is that CGContextDrawPDFPage leaks in iOS versions prior to 6.0.
The release needs to come after the page has been used, not before. So first, move CGPDFPageRelease(page) to last in this code block and see if that helps. Also, the problem could have something to do with the CGPDFDocumentRef stored in the pdf variable. If the above doesn't help, it would be good if you show how you obtain the reference, and where you retain and release it.

UIGraphicsGetImageFromCurrentImageContext memory leak with previews

I'm trying to create previews images of pages in a PDF
but I have some problems with the release of memory.
I wrote a simple test algorithm that cycles on the problem,
the app crashes near the 40th iteration:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:#"myPdf.pdf"];
CFURLRef url = CFURLCreateWithFileSystemPath( NULL, (CFStringRef)pdfPath, kCFURLPOSIXPathStyle, NO );
CGPDFDocumentRef myPdf = CGPDFDocumentCreateWithURL( url );
CFRelease (url);
CGPDFPageRef page = CGPDFDocumentGetPage( myPdf, 1 );
int i=0;
while(i < 1000){
UIGraphicsBeginImageContext(CGSizeMake(768,1024));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context,CGRectMake(0, 0, 768, 1024));
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, 1024);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
// --------------------------
// The problem is here (without this line the application doesn't crash)
UIImageView *backgroundImageView1 = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
// --------------------------
UIGraphicsEndImageContext();
[backgroundImageView1 release];
NSLog(#"Loop: %d", i++);
}
CGPDFDocumentRelease(myPdf);
The above-mentioned line seems to generate a memory leak,
however, instruments doesn't show memory problems;
Can I escape from this kind of mistake?someone can explain me in which way?
Are there other ways to show previews of a pdf?
UPDATE
I think the problem isn't the release of UIImage created by the method UIGraphicsGetImageFromCurrentImageContext() but the release of UIImageView created with this autorelease image.
I have divided the line of code in three steps:
UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *myImageView = [[UIImageView alloc] init];
[myImageView setImage: myImage]; // Memory Leak
The first and second lines doesn't create memory leaks so I think that the method UIGraphicsGetImageFromCurrentImageContext is not the problem.
I also tried as follows but the problem persists:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage];
I think there is a memory leak in the release of a UIImageView that contains a UIImage with the autorelease property.
I tried to write my object UIImageView inheriting a UIView as explained in this thread.
This solution works but isn't very elegant, it's a workaround, I would prefer to use the object UIImageView solving the memory problem.
The problem is this:
UIGraphicsGetImageFromCurrentImageContext()
returns an autoreleased UIImage. The autorelease pool holds on to this image until your code returns control to the runloop, which you do not do for a long time. To solve this problem, you would have to create and drain a fresh autorelease pool on every iteration (or every few iterations) of your while loop.
I know it's an old question, but I've just been banging my head against the wall on this for a few hours. In my app repeatedly calling
UIImage *image = UIGraphicsGetImageFromCurrentImageContext()
in a loop does hold on to the memory despite me calling image = nil; Not sure how long the app would keep hold of the memory before freeing, but it's certainly long enough for my app to get a memory warning then crash.
I managed to solve it finally by wrapping the code that calls / uses the image from UIGraphicsGetImageFromCurrentImageContext() in #autoreleasepool. So I have:
#autoreleasepool {
UIImage *image = [self imageWithView:_outputImageView]; //create the image
[movie addImage:image frameNum:i fps:kFramesPerSec]; //use the image as a frame in movie
image = nil;
}
Hope that might help someone.
For future reference here's what I did to solve this (tested in Swift 4).
I was calling the function below for every new image downloaded from the internet (on a utility queue). Before implementing the autorelease pool it would crash after processing about 100.
For simplicity, in the resizeImage function I've removed needed code except for the autoreleasepool and the part that was leaking.
private func resizeImage(image: UIImage, toHeight: CGFloat) -> UIImage {
return autoreleasepool { () -> UIImage in
[...]
let newImage = UIGraphicsGetImageFromCurrentImageContext() //Leaked
UIGraphicsEndImageContext()
return newImage!
}
}
I hope this helps!
For those who tried all solution above and still has a memory leak, check if you are using a dispatch queue. If so, be sure to set its autoreleaseFrequency to .workItem. Or the autorelease pool you set up inside the will not execute.
DispatchQueue(label: "imageQueue", qos: .userInitiated, autoreleaseFrequency: .workItem)
Hope it helps, it has bugged me for hours until I finally realize that's DispatchQueue that is holding the block.
Is this code running on the main thread? The documentation of the UIGraphicsGetImageFromCurrentImageContext (link) says it must run that way.
your line of crash you can update it like following
get one UIimage out of loop
rendered_image = UIGraphicsGetImageFromCurrentImageContext();

How can I programmatically generate a thumbnail of a PDF with iOS?

We're displaying PDF content using UIWebViews at the moment. Ideally I would like to be able to display thumbnails in the UITableView without loading many different UIWebViews concurrently... they're slow enough loading one document - never mind 10+!
How do I go about doing this?
I've thought about screen capturing a document loaded using UIDocumentInteractionController or UIWebView but this means they'd all have to be thumbnailed before displaying the table.
Apple supply a whole bunch of methods down at the CoreGraphics level for drawing PDF content directly. As far as I'm aware, none of it is neatly packaged up at the UIKit level, so it may not be a good fit for your project at this point, especially if you're not as comfortable down at the C level. However, the relevant function is CGContextDrawPDFPage; per the normal CoreGraphics way of things there are other methods to create a PDF reference from a data source and then to get a page reference from a PDF. You'll then need to deal with scaling and translating to the view you want, and be warned that you'll need to perform a horizontal flip because PDFs (and OS X) use the lower left as the origin whereas iOS uses the top left. Example code, off the top of my head:
UIGraphicsBeginImageContext(thumbnailSize);
CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithProvider( (CGDataProviderRef)instanceOfNSDataWithPDFInside );
CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdfRef, 1); // get the first page
CGContextRef contextRef = UIGraphicsGetCurrentContext();
// ignore issues of transforming here; depends on exactly what you want and
// involves a whole bunch of normal CoreGraphics stuff that is nothing to do
// with PDFs
CGContextDrawPDFPage(contextRef, pageRef);
UIImage *imageToReturn = UIGraphicsGetImageFromCurrentImageContext();
// clean up
UIGraphicsEndImageContext();
CGPDFDocumentRelease(pdfRef);
return imageToReturn;
At a guess, you'll probably want to use CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox) to get the page's crop box and then work out how to scale/move that to fit your image size.
Probably easier for your purposes is the -renderInContext: method on CALayer (see QA 1703) — the old means of getting a screenshot was UIGetScreenImage, but that was never really official API and was seemingly temporarily allowed only because of the accidental approval of RedLaser. With the code in the QA you can rig yourself up to get a UIImage from any other view without that view having to be on screen. Which possibly resolves some of your issue with screen capturing? Though it means you can support OS 4.x only.
In either case, PDFs just don't draw that quickly. You probably need to populate the table then draw the thumbnails on a background thread, pushing them upwards when available. You can't actually use UIKit objects safely on background threads but all the CoreGraphics stuff is safe.
Here is sample code considering transformation. :)
NSURL* pdfFileUrl = [NSURL fileURLWithPath:finalPath];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfFileUrl);
CGPDFPageRef page;
CGRect aRect = CGRectMake(0, 0, 70, 100); // thumbnail size
UIGraphicsBeginImageContext(aRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage* thumbnailImage;
NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf);
for(int i = 0; i < totalNum; i++ ) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, aRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetGrayFillColor(context, 1.0, 1.0);
CGContextFillRect(context, aRect);
// Grab the first PDF page
page = CGPDFDocumentGetPage(pdf, i + 1);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, aRect, 0, true);
// And apply the transform.
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, page);
// Create the new UIImage from the context
thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
//Use thumbnailImage (e.g. drawing, saving it to a file, etc)
CGContextRestoreGState(context);
}
UIGraphicsEndImageContext();
CGPDFDocumentRelease(pdf);
Here is a swift 3 method for generating a UIImage thumbnail for a pdf page
static func getThumbnailForPDF(_ urlString:String, pageNumber:Int) -> UIImage? {
let bundle = Bundle.main
let path = bundle.path(forResource: urlString, ofType: "pdf")
let pdfURL = URL(fileURLWithPath: path!)
let pdf = CGPDFDocument(pdfURL as CFURL )!
let page = pdf.page(at: pageNumber)!
let rect = CGRect(x: 0, y: 0, width: 100.0, height: 70.0) //Image size here
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()!
context.saveGState()
context.translateBy(x: 0, y: rect.height)
context.scaleBy(x: 1.0, y: -1.0)
context.setFillColor(gray: 1.0, alpha: 1.0)
context.fill(rect)
let pdfTransform = page.getDrawingTransform(CGPDFBox.mediaBox, rect: rect, rotate: 0, preserveAspectRatio: true)
context.concatenate(pdfTransform)
context.drawPDFPage(page)
let thumbImage = UIGraphicsGetImageFromCurrentImageContext()
context.restoreGState()
UIGraphicsEndImageContext()
return thumbImage
}
If you use PDFKit, there is a method for PDFPage called thumbnail. It is available in iOS11 and above and does the job in just a few lines of code. Here is a simple method for generating a thumbnail of a given page in a PDFDocument.
func makeThumbnail(pdfDocument: PDFDocument?, page: Int) -> UIImage? {
return pdfDocument?.page(at: page)?.thumbnail(of: CGSize(width: 40, height: 40), for: .artBox)
}
Unfortunately it misses official documentation (see, https://developer.apple.com/documentation/pdfkit/pdfpage/2869834-thumbnail). However, there are some comments accessible in the code for PDFPage.

Resources