【问题标题】:UIImage returned from UIGraphicsGetImageFromCurrentImageContext leaks从 UIGraphicsGetImageFromCurrentImageContext 返回的 UIImage 泄漏
【发布时间】:2011-11-18 11:54:06
【问题描述】:

Instruments Tool 中的 Leak Profiling 截图:http://i.stack.imgur.com/rthhI.png

我发现我的 UIImage 对象使用 Instruments 工具泄漏。 根据 Apple 的文档,从 UIGraphicsGetImageFromCurrentImageContext 返回的对象应该是autoreleased,我还可以在分析时看到“Autorelease”事件(参见我所附屏幕截图的前两行历史记录)。但是,似乎“自动释放”事件无效。为什么?

编辑:

附加代码,我使用下面的代码来“混合”两个 UIImage,稍后,我使用 UIMutableDictionary 来缓存那些我“混合”的 UIImage。而且我很确定我已经调用了 [UIMutableDictionary removeAllObjects] 来清除缓存,所以那些 UIImages “应该被清除”

+ (UIImage*) mixUIImage:(UIImage*)i1 :(UIImage*)i2 :(CGPoint)i1Offset :(CGPoint)i2Offset{
CGFloat width , height;
if (i1) {
    width = i1.size.width;
    height = i1.size.height;
}else if(i2){
    width = i2.size.width;
    height = i2.size.height;
}else{
    width = 1;
    height = 1;
}

// create a new bitmap image context
//
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, i1.scale);

// get context
//
CGContextRef context = UIGraphicsGetCurrentContext();       

// push context to make it current 
// (need to do this manually because we are not drawing in a UIView)
//
UIGraphicsPushContext(context);                             

// drawing code comes here- look at CGContext reference
// for available operations
//
// this example draws the inputImage into the context
//
[i2 drawInRect:CGRectMake(i2Offset.x, i2Offset.y, width, height)];
[i1 drawInRect:CGRectMake(i1Offset.x, i1Offset.y, width, height)];


// pop context 
//
UIGraphicsPopContext();                             

// get a UIImage from the image context- enjoy!!!
//
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

// clean up drawing environment
//
UIGraphicsEndImageContext();

return outputImage;

}

【问题讨论】:

标签: iphone ios uiimage autorelease


【解决方案1】:

我在使用来自 UIGraphicsGetImageFromCurrentImageContext() 的保留 UIImage 图像时遇到了奇怪的 UIImage 内存泄漏。我在后台线程中调用它(响应计时器事件)。问题原来是——正如苹果在文档中所提到的——“你应该只从你的应用程序的主线程调用这个函数”。小心。

【讨论】:

  • 从 iOS 4 开始,您可以从任何线程调用 UIGraphicsGetImageFromCurrentImageContext(),所以这应该不再是问题了。 link
猜你喜欢
  • 2011-11-03
  • 2013-11-26
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多