- [UIImage drawInRect:] / CGContextDrawImage () does not free memory?

I wanted to easily compose UIImage on top of another background image, so I wrote a category method for UIImage adapted from mix two uiimages based on alpha / transparency of the top image :

- (UIImage *) blendedImageOn:(UIImage *) backgroundImage  {
 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
 UIGraphicsBeginImageContext(backgroundImage.size);

 CGRect rect = CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height);
 [backgroundImage drawInRect:rect];
 [self drawInRect:rect];
 UIImage* blendedImage = [UIGraphicsGetImageFromCurrentImageContext() retain];

 UIGraphicsEndImageContext();
 [pool release];

 return [blendedImage autorelease];
}

Unfortunately, my application using the above method downloads about 20 images and mixes them with background and glossy images (maybe about 40 calls), is dropped to the device.

"" , malloc, drawInRect: . drawInRect: CGContextDrawImage, . AutoReleasePool , ; .

, , , , . - , ? - ?

- 1: . , 20 , :

    UIImage *blendedImage = [newImage blendedImageOn:backgroundImage];

, , , , UIImages , . , .

- 2: , blendedImageOn: , .

- 3: ​​ UIImage, - . , UIImage, , , , .

, ( , !): 20 ?

+3
4

. , , , , , .

, .

+1

drawRect: . [self setNeddsDisplay]; ( );

. . autotleased UIImage. , UIImage, . , , blendedImageOn: 3-5 .

+1

, blendedImage, , , , poof, blendedImage.

, , :

...
UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext();
[blendedImage retain];       // keep this past pool release
[pool release];

[blendedimage autorelease];  // now it will be autoreleased from the main pool

return [blendedImage release];  // release to balance out the retain above
0

iPhone, , "" ( ), "alloc", "", "".

Your name "blendedImageOn" does not meet these guidelines. If you use it after a month or someone else uses it, you will not remember that the image is stored inside and you will have a memory leak.

-1
source

Source: https://habr.com/ru/post/1748244/


All Articles