问题
I need to draw a series of large UIImages on top of each other and I am coming up against memory issues and I was wondering if anybody has a solution.
The code below gives you an example of how my function calls work. The functions have been simplified to show my main issue.
The function "generateImage" has a for loop that calls into a subclass that also returns images that have been generated from a series of paths. I have put @autoreleasepools around the calls so that the images that are generated are immediately deallocated after they are used, but this does not seem to be working and when arrayOfSubImages grows I come up against memory issues.
-( UIImage *) generateImage
{
@autoreleasepool {
UIGraphicsBeginImageContextWithOptions( CGSizeMake( 1000, 1000 ), NO, 0.0 );
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
for ( SubImage *subImage in arrayOfSubImages ) {
UIImage *aImage = [ subImage imageFromPaths ];
[ aImage drawAtPoint:CGPointZero ];
}
UIGraphicsPopContext();
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
}
The SubImage that is called from generateImage
-( UIImage *) imageFromPaths
{
@autoreleasepool {
UIGraphicsBeginImageContextWithOptions( CGSizeMake( 1000, 1000 ), NO, 0.0 );
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
for ( UIBezierPath *path in arrayOfPaths ) {
[ path fill ];
}
UIGraphicsPopContext();
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
}
Any help would be appreciated
Reza
回答1:
I've had similar problems in the past. Try adding an @autoreleasepool within your for-loops because you're likely running into memory problems by performing several memory intensive operations within a tight loop. Memory won't automatically release itself until a loop is over even if that loop is contained in an autoreleasepool -- you need to release each individual iteration.
回答2:
Setting the images to nil directly after using them should release the memory, like so:
-( UIImage *) generateImage
{
@autoreleasepool {
UIGraphicsBeginImageContextWithOptions( CGSizeMake( 1000, 1000 ), NO, 0.0 );
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
for ( SubImage *subImage in arrayOfSubImages ) {
UIImage *aImage = [ subImage imageFromPaths ];
[ aImage drawAtPoint:CGPointZero ];
aImage = nil;
}
UIGraphicsPopContext();
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
回答3:
Instead using temp variables, Like:
UIimage * image=
You can define an instance temp variables in you .h file, Like:
UIImage * _image;
Then use it everywhere you define a new image, which needs to be deallocated. It can guarantee you reset memory in time.
Scott is right. I forgot this issue. Release within Loop is the key
来源:https://stackoverflow.com/questions/20304615/drawing-a-series-of-uiimages-and-memory-issues