iOS UIAppearance Error
Asked Answered
J

3

6

I'm building a file management app, and I occasionally get the following error while calling a UIImagePickerController or a MPMediaPickerController:

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

I recently gave my app a custom theme using iOS 5's UIAppearance API and thats when I started getting this error. By guessing and checking, I found the problematic lines of my code that cause this error:

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];

I have no idea how this code triggers the above error. Can you please explain to me the source of this error and provide a solution to fix it.

Thanks in advance for your help, Guvvy

Jackfruit answered 21/6, 2012 at 3:28 Comment(3)
For those unlucky enough to be seeing this crash, here's another very useful resource: openradar.appspot.com/11411000Sightly
do we know why this happens yet? I'm getting the same thing in iOS 6. I am custom drawing my own 20x20 images and have cap insets of 9 on all sides...Richthofen
@Richthofen are you getting this crash on non-retina devices?Jackfruit
J
3

After some more thorough testing, I have reached the conclusion that this problem is limited to retina devices. The issue turned out to be in the @2x images. They had a odd numbered resolution (eg. 59px by 60px). All I did was recreate the image and changed the resolution to 60px by 60px and I never experienced the problem again.

I was kind of surprised by the solution as I saw no correlation between the error message and the line of code, but in the end, it was the images that caused this problem.

Jackfruit answered 21/6, 2012 at 3:46 Comment(1)
My @2x images dont have odd numbered resolution but this crash still occurs occasionally...Noheminoil
S
2

I had a similar problem, but my crash was caused by a resizable image in a UIImageView.

My resizable image has edge insets of top=30px, left=20px, bottom=1px, right=10px. The image is 43x45, so its resizable area is 13x14. I'm using iOS6, so I was able to workaround the problem by specifying UIImageResizingModeStretch as the resizingMode for -[UIImage resizableImageWithCapInsets:resizingMode:].

Works:

UIImage *image = [UIImage imageNamed:name];
UIImage *resizableImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:UIImageResizingModeStretch];

Crashes with EXC_BAD_ACCESS or [_UIImageViewPretiledImageCacheKey hash]: message sent to deallocated instance 0xb14deb0 with NSZombieEnabled:

UIImage *image = [UIImage imageNamed:name];
UIImage *resizableImage = [image resizableImageWithCapInsets:edgeInsets];
Shirring answered 11/1, 2013 at 4:51 Comment(0)
G
0

I got the same for the following code

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

where self is UITableViewCell and stretchableImageNamed: is simply

+(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)];
}

this helped:

self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage stretchableImageNamed:@"imageName"]];
self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage stretchableImageNamed:@"imageName"]];
Grieve answered 21/2, 2013 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.