renderincontext内存泄漏,如果不在主线程上使用 [英] renderincontext Memory Leak if not use on Main thread
问题描述
我正在尝试使用下面的代码将我的UIView转换为UIImage。
+(UIImage *)imageWithView:(UIView *)view {
float scale = 1.0f;
UIGraphicsBeginImageContextWithOptions(view.bounds.size,YES,scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
view.layer.contents = nil;
return img;
}
此代码存在两个问题。
1。当我在后台线程中运行此代码时(!mainThread)
当在后台线程中调用renderInContext时,我遇到内存泄漏问题。
2。当我在主线程上运行此代码时
没有内存泄漏但在iPad 3上我遇到了一些性能问题(当这个方法是在从UIView创建图像时调用)。因为我需要在几秒钟内调用此函数超过5次,因此UI挂起会给用户带来非常糟糕的体验。
如果我在这里做错了什么,请指导我?
我认为问题1与 UIKit
的事实有关不是线程安全的,它的使用会导致各种副作用。
如果你遇到性能问题就像你在描述一样,我看到的唯一前进道路是直接使用在辅助线程上的CoreGraphics(不是UIKit)。
您可以尝试这样的事情作为开始:
size_t width = view.bounds.size.width;
size_t height = view.bounds.size.height;
unsigned char * imageBuffer =(unsigned char *)malloc(width * height * 4);
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef imageContext =
CGBitmapContextCreate(imageBuffer,width,height,8,width * 4,colourSpace,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
CGColorSpaceRelease(colourSpace);
[view.layer renderInContext:imageContext];
CGImageRef outputImage = CGBitmapContextCreateImage(imageContext);
CGImageRelease(outputImage);
CGContextRelease(imageContext);
免费(imageBuffer);
如你所见,这比 I am trying to convert my UIView into UIImage using below code. There are two problem with this code. 1. When I run this code in background thread(!mainThread) I faced memory leak problem when renderInContext is called in background thread. 2. When I run this code on Main thread There is no memory leak but on iPad 3 I am facing some performance issue (my UI hangs when this method is called) while creating image from UIView. As I need to call this function more than 5 times in a seconds so UI hangs gives very bad user experience. Please guide me if I am doing some thing wrong here? I think that issue 1 is related to the fact that If you have performance issue like you are describing, the only path forward I see is directly using CoreGraphics (not UIKit) on a secondary thread. You might try something like this, as a start: As you see, this is pretty more complex than the 这篇关于renderincontext内存泄漏,如果不在主线程上使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! UIKit $复杂得多c $ c>方式,但它可以在辅助线程上运行(假设您找到了将
outputImage
传递回未显示的UI线程的方法)。 / p> + (UIImage *) imageWithView:(UIView *)view{
float scale = 1.0f;
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
view.layer.contents = nil;
return img;
}
UIKit
is not thread safe and its use will lead to all kinds of side effects.size_t width = view.bounds.size.width;
size_t height = view.bounds.size.height;
unsigned char *imageBuffer = (unsigned char *)malloc(width*height*4);
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef imageContext =
CGBitmapContextCreate(imageBuffer, width, height, 8, width*4, colourSpace,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
CGColorSpaceRelease(colourSpace);
[view.layer renderInContext:imageContext];
CGImageRef outputImage = CGBitmapContextCreateImage(imageContext);
CGImageRelease(outputImage);
CGContextRelease(imageContext);
free(imageBuffer);
UIKit
way, but it can be run on a secondary thread (provided you find a way to pass the outputImage
back to your UI thread which is not shown).