renderincontext内存泄漏,如果不在主线程上使用 [英] renderincontext Memory Leak if not use on Main thread

查看:908
本文介绍了renderincontext内存泄漏,如果不在主线程上使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下面的代码将我的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);

如你所见,这比 UIKit 方式,但它可以在辅助线程上运行(假设您找到了将 outputImage 传递回未显示的UI线程的方法)。 / p>

I am trying to convert my UIView into UIImage using below code.

+ (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;
}

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 UIKit is not thread safe and its use will lead to all kinds of side effects.

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:

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);

As you see, this is pretty more complex than the 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).

这篇关于renderincontext内存泄漏,如果不在主线程上使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆