iOS UIAppearance 错误

6

我正在开发一个文件管理应用,有时在调用UIImagePickerControllerMPMediaPickerController时会出现以下错误:

*** -[_UIImageViewPretiledImageCacheKey hash]: message sent to deallocated instance 0x140dc0

我最近使用iOS 5的UIAppearance API为我的应用程序提供了自定义主题,然后我开始遇到这个错误。通过猜测和检查,我找到了导致此错误的代码行:

UIImage *backButtonImage = [[UIImage imageNamed:@"backButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(12, 16, 12, 8)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
UIImage *barButtonImage = [[UIImage imageNamed:@"barButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(14, 12, 14, 12)];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

我不知道这段代码是如何引发以上错误的。你能否解释一下这个错误的来源并提供解决方案呢?
非常感谢您的帮助, Guvvy

2
对于那些不幸遭遇崩溃的人,这里有另一个非常有用的资源:http://openradar.appspot.com/11411000 - jpm
我们现在知道这是为什么吗?我在iOS 6中也遇到了同样的问题。我正在自定义绘制自己的20x20图像,并在所有边缘上具有9个cap插图... - bogardon
@bogardon,你在非Retina设备上也遇到了这个崩溃吗? - Gavy
3个回答

3
经过更加深入的测试,我得出结论,这个问题仅限于视网膜设备。问题出现在@2x图片上。它们的分辨率为奇数(例如59px乘60px)。我所做的只是重新创建图像,并将分辨率更改为60px乘60px,之后再也没有遇到过这个问题。
我对解决方案感到有些惊讶,因为我没有看到错误消息和代码行之间的关联,但最终,是图像导致了这个问题。

我的@2x图像没有奇数分辨率,但这个崩溃有时仍会发生... - user1920855

2
我有一个类似的问题,但我的崩溃是由于UIImageView中的可调整大小的图像引起的。
我的可调整大小的图像具有上30像素、左20像素、下1像素、右10像素的边缘插图。该图像为43x45,因此其可调整大小区域为13x14。我正在使用iOS6,因此我能够通过将UIImageResizingModeStretch指定为-[UIImage resizableImageWithCapInsets:resizingMode:]的resizingMode来解决问题。
运作良好:
UIImage *image = [UIImage imageNamed:name];
UIImage *resizableImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:UIImageResizingModeStretch];

出现 NSZombieEnabledEXC_BAD_ACCESS[_UIImageViewPretiledImageCacheKey hash]: message sent to deallocated instance 0xb14deb0 崩溃:
UIImage *image = [UIImage imageNamed:name];
UIImage *resizableImage = [image resizableImageWithCapInsets:edgeInsets];

0

以下代码我得到了相同的结果。

UIImage* image = [UIImage stretchableImageNamed:@"imageName"];
self.backgroundView = [[UIImageView alloc] initWithImage:image];
self.selectedBackgroundView = [[UIImageView alloc] initWithImage:image];

其中selfUITableViewCell,而stretchableImageNamed:只是简单的方法。

+(UIImage*)stretchableImageNamed:(NSString*)name
{
    UIImage *img = [UIImage imageNamed:name];
    CGSize sz = img.size;
    int left = (sz.width - 1.) / 2.;
    int top = (sz.height - 1.) / 2.;
    return [img resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, top, left)];
}

这有所帮助:

self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage stretchableImageNamed:@"imageName"]];
self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage stretchableImageNamed:@"imageName"]];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接