【发布时间】: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