仪器工具中的泄漏分析的屏幕截图:http://i.stack.imgur.com/rthhI.png
我发现我的UIImage对象使用了Instruments工具泄漏。 根据Apple的文档,从UIGraphicsGetImageFromCurrentImageContext返回的对象应该是自动释放,我还可以在分析时看到“自动释放”事件(请参阅我附加截图的前两行历史记录)。但是,似乎“自动释放”事件不起作用。为什么呢?
编辑:
附加代码,我使用下面的代码“混合”两个UIImages,稍后,我使用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;
}
答案 0 :(得分:-1)
我使用来自UIGraphicsGetImageFromCurrentImageContext()的保留UIImage图像获得了一个奇怪的UIImage内存泄漏。我在后台线程中调用它(响应计时器事件)。问题结果是 - 正如苹果文档中深入提到的那样 - “你应该只从你的应用程序的主线程中调用这个函数”。当心。