Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

major memory problem/crash with UIImage

Hi,


Our app needs to render the screen to an UIImage and everytime we do that, the memory utilization of app goes up by about 0.3M [as shown by XCode execution panel, but the Instruments panel shows 3M - see below] eventually causing memory warning and crashing.


The code used is simple - from the example code of Apple:


UIGraphicsBeginImageContext(self.view.bounds.size);

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();


I've tried many ways to reclaim this memory, but there is unbounded growth.

We've checked it many times and there are no leaks, etc.

It is not allocated using imageNamed so I understand it is not cached - regardless we even called [super didReceiveMemoryWarning] to try to flush the cache, but no luck.


In XCode instruments, the allocations show huge nearly 3MB growth as below:


#Event Type∆ RefCtRefCtTimestampResponsible LibraryResponsible Caller
0VM Alloc02:08.145.871CoreGraphicsCGDataProviderCreateWithCopyOfData



The LiveBytes increases by 3MB everytime the UIImage is allocated and RELEASED by the app.


Graph CategoryLive Bytes# Living# TransientOverall Bytes# OverallBytes Allocated (Net / Overall)
1All Heap & Anonymous VM12.16 MB112633422178597.30 MB3433441<XRRatioObject: 0x7fbca501d950> %0.02, %0.98



WHY is this NOT RELEASED by iOS? Can someone help - our app is crashing and rendering the screen to an image is a core part of the functionality.


Thanks,

RFS

iPhone 5, iOS 7.0.1

Posted on Sep 27, 2013 3:27 AM

Reply
4 replies

Jan 13, 2014 6:19 PM in response to RedFiveSeven

I am having the same problem, it is leaking all the memory. It works fine on iOS 6, but it is causing me Memory Warnings on iOS 7.

This is my code



CGImageRef imageRef = image.CGImage;

// System only supports RGB, set explicitly and prevent context error

// if the downloaded image is not the supported format

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();


CGContextRef context = CGBitmapContextCreate(NULL,

CGImageGetWidth(imageRef),

CGImageGetHeight(imageRef),

8,

// width * 4 will be enough because are in ARGB format, don't read from the image

CGImageGetWidth(imageRef) * 4,

colorSpace,

// kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little

// makes system don't need to do extra conversion when displayed.

kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);

CGColorSpaceRelease(colorSpace);


if ( ! context) {

return nil;

}


CGRect rect = (CGRect){CGPointZero, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)};

CGContextDrawImage(context, rect, imageRef);

CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context);

CGContextRelease(context);


UIImage *decompressedImage = [[UIImage alloc] initWithCGImage:decompressedImageRef];

CGImageRelease(decompressedImageRef);


return [decompressedImage autorelease];

major memory problem/crash with UIImage

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.