开发者

Converting an UIview unto UIimage causing memory leak

开发者 https://www.devze.com 2022-12-11 22:30 出处:网络
I\'m developing an app for iPhone using a coverFlow view, when the app is building the cards it is usinga UIView in order to add labels and other stuff. Then I convert the UIView into UIImage using th

I'm developing an app for iPhone using a coverFlow view, when the app is building the cards it is using a UIView in order to add labels and other stuff. Then I convert the UIView into UIImage using the following code:

UIGraphicsBeginImageContext(imageView.bounds.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// returning the UIImage
return viewImage;

Every Time I redraw the coverflow I have a huge memory allocation increment, that never decreases even if I dealloc my coverFlow view.

I think 开发者_Python百科the memory leak is in the code that I added, what do you think?


There is no memory leak apparent in the code snippet you provided. That operation could not be performed on a background thread because of UIGraphicsBeginImageContext(), so you should have an NSAutoreleasePool in place (the return value of UIGraphicsGetImageFromCurrentContext() is autoreleased). Without further information, its impossible to tell where the memory leak could be - I suggest you look at whatever objects eventually own the viewImage object and make sure you are properly releasing the UIImage if you retain it.


Use drawViewHierarchyInRect:afterScreenUpdates: instead of renderInContext: it is 15x faster.

Converting an UIview unto UIimage causing memory leak

You can see the comparison on this article.

Also, I have created a Swift extension for doing this: https://stackoverflow.com/a/32042439/517707

0

精彩评论

暂无评论...
验证码 换一张
取 消