Iphone 如何确保UIImage永远不会发布?
我从iPhone上抓到了崩溃日志:Iphone 如何确保UIImage永远不会发布?,iphone,memory-management,uiimageview,uiimage,Iphone,Memory Management,Uiimageview,Uiimage,我从iPhone上抓到了崩溃日志: Exception Type: EXC_BAD_ACCESS (SIGBUS) Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000c Crashed Thread: 0 Thread 0 Crashed: 0 libobjc.A.dylib 0x30011940 objc_msgSend + 20 1 CoreFoundation
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000c
Crashed Thread: 0
Thread 0 Crashed:
0 libobjc.A.dylib 0x30011940 objc_msgSend + 20
1 CoreFoundation 0x30235f1e CFRelease + 98
2 UIKit 0x308f4974 -[UIImage dealloc] + 36
3 CoreFoundation 0x30236b72 -[NSObject release] + 28
4 UIKit 0x30a00298 FlushNamedImage + 64
5 CoreFoundation 0x30250a20 CFDictionaryApplyFunction + 124
6 UIKit 0x30a0019c _UISharedImageFlushAll + 196
7 UIKit 0x30a00730 +[UIImage(UIImageInternal) _flushCacheOnMemoryWarning:] + 8
8 Foundation 0x3054dc7a _nsnote_callback + 178
9 CoreFoundation 0x3024ea52 _CFXNotificationPostNotification + 298
10 Foundation 0x3054b854 -[NSNotificationCenter postNotificationName:object:userInfo:] + 64
11 Foundation 0x3054dbba -[NSNotificationCenter postNotificationName:object:] + 14
12 UIKit 0x30a00708 -[UIApplication _performMemoryWarning] + 60
13 UIKit 0x30a006a0 -[UIApplication _receivedMemoryNotification] + 128
14 UIKit 0x30a005d0 _memoryStatusChanged + 56
15 CoreFoundation 0x30217410 __CFNotificationCenterDarwinCallBack + 20
16 CoreFoundation 0x3020d0aa __CFMachPortPerform + 72
17 CoreFoundation 0x30254a70 CFRunLoopRunSpecific + 2296
18 CoreFoundation 0x30254164 CFRunLoopRunInMode + 44
19 GraphicsServices 0x3204529c GSEventRunModal + 188
20 UIKit 0x308f0374 -[UIApplication _run] + 552
21 UIKit 0x308eea8c UIApplicationMain + 960
...
...
从上一个问题开始,我主要围绕UIImage部分更改了代码。我现在使用[[UIImage alloc]initWithContentsOfFile…]。不再有[UIImage ImageName:…]或类似内容。这部分内容如下
//this is a method of a subclass of UIImageView.
- (void) reviewImage: (bool) review{
NSString* st;
if (review){
NSString* origin = [NSString stringWithString: [[ReviewCardManager getInstance] getCardImageString:chosenIndex]];
NSString* stt = [origin substringToIndex: [origin length]-4];
st = [[NSString alloc] initWithString: stt];
if (myImageFlipped == nil)
myImageFlipped = [[UIImage alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:st ofType:@"png"]];
[self setImage:myImageFlipped];
if (notRotated){
self.transform = CGAffineTransformRotate(self.transform, [MyMath radf:rotate]);
notRotated = false;
}
}else{
st = [[NSString alloc] initWithFormat:@"sc%d", chosenNumber];
if (myImage == nil)
myImage = [[UIImage alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:st ofType:@"png"]];
[self setImage:myImage];
if (notRotated){
self.transform = CGAffineTransformRotate(self.transform, [MyMath radf:rotate]);
notRotated = false;
}
}
[st release];
}
我还将UIImage保留在该属性中
@property (nonatomic, retain) UIImage* myImage, *myImageFlipped;
内存泄漏也得到了处理。这些变量在dealloc方法中发布
我以为我已经成功地杀死了这个bug,但似乎我仍然有一个罕见的bug问题
根据崩溃日志,我的应用程序会发出performMemoryWarning。我只是分配了大小为156 x 272的13.png图像。我很困惑。这些图像不应该占用太多内存,以至于超过iPhone的RAM。还是我忽略了什么?请告知。为了帮助您解决内存问题和UIImage,您可能需要使用UIImage的imageNamed Convenience方法,文档如下: 此方法在系统缓存中查找具有指定名称的图像对象,并返回该对象(如果存在)。如果匹配的图像对象不在缓存中,此方法将从指定的文件加载图像数据,对其进行缓存,然后返回结果对象。
或者,如果切换到UIImage ImageName后仍遇到内存问题,您可能希望继续,因为使用便捷方法有一些缺点。问题已经解决。我忘了在一个地方更改UIImage。现在,所有UIImage都是真正的alloc,不再自动释放
仅供参考,如果您使用的是[UIImage ImageName:…],请在iPhone模拟器上使用Simulate Memory Warning,查看在实际设备内存不足时您是否遇到问题。否,海报上已经提到他们已从ImageName移走,这是我为解决这些内存警告而建议的,因为imageNamed的缓存超出了程序员的控制。啊,我读了那部分。在这种情况下,像链接的博客文章中那样的自定义缓存将更有意义。您的属性似乎名为myImage,而您的setter名为setImage:。你能解释一下这里发生了什么吗?我正在对UIImageView进行子类化,这个类有2个UIImage。当我想更改UIImageView的图像时,我基本上调用setImage。