【发布时间】:2010-11-28 21:57:29
【问题描述】:
喜欢这篇文章:
我遇到了类似的问题。 create_bitmap_data_provider 中 malloc 的指针永远不会被释放。我已经验证了关联的图像对象最终被释放,而不是提供者的分配。我应该明确创建一个数据提供者并以某种方式管理它的内存吗?看起来像个黑客。
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, blah blah blah);
CGColorSpaceRelease(colorSpace);
// ... draw into context
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage * image = [[UIImage alloc] initWithCGImage:imageRef];
CGImageRelease(imageRef);
CGContextRelease(context);
在fbrereto下面的回答之后,我将代码更改为:
- (UIImage *)modifiedImage {
CGSize size = CGSizeMake(width, height);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
// draw into context
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image; // image retainCount = 1
}
// caller:
{
UIImage * image = [self modifiedImage];
_imageView.image = image; // image retainCount = 2
}
// after caller done, image retainCount = 1, autoreleased object lost its scope
不幸的是,这仍然会出现相同的问题,即水平翻转图像的副作用。它似乎在内部对 CGBitmapContextCreateImage 做同样的事情。
我已验证我的对象的 dealloc 已被调用。在我释放_imageView 之前,_imageView.image 和_imageView 上的retainCount 都是1。这真的没有意义。其他人似乎也有这个问题,我是最后一个怀疑SDK的人,但是这里会不会有iPhone SDK的错误???
【问题讨论】:
-
不要使用 UIGraphicsBeginImageContext:在多线程应用程序中不安全。
-
知道了,我还是把它拿出来做水平图像翻转。
标签: iphone uiimage memory-leaks