Darren
Darren

Reputation: 10398

resizableImageWithCapInsets:resizingMode: crashing on iOS 5.1

I'm using this code to stretch an image correctly, however on iOS 5.1 it crashes. If I remove the resizingMode from the end, it works but the image is then tiled and looks funny. Any ideas why it's crashing?

Thanks

self.scrollViewImage.image = [[UIImage imageNamed:@"SysInfoBackBox"] resizableImageWithCapInsets:UIEdgeInsetsMake(40, 40, 40, 40) resizingMode:UIImageResizingModeStretch];

Upvotes: 4

Views: 4255

Answers (3)

Manu
Manu

Reputation: 788

I replayed to another question that should be related to your problem https://stackoverflow.com/a/14623534/2028575

but if you like I can put here the sare answer:

this was happening only with devices with iOS5.x resizing an UIImageView that diplay an UIImage created in this way:

    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(topCapHeight, leftCapWidth, topCapHeight, leftCapWidth);
    image = [originalImage resizableImageWithCapInsets:edgeInsets];

this is probably an iOS bug that has been fixed in iOS6.x

if your case is resizing the image with a mirror criteria you can use this way:

create a category of UIImage and add this instance method:

- (UIImage*)resizableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight </b>
{
    UIImage *image = nil;    
    float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (osVersion < 6.0) {
        image = [self stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
    } else {
        UIEdgeInsets edgeInsets = UIEdgeInsetsMake(topCapHeight, leftCapWidth, topCapHeight, leftCapWidth);
        image = [self resizableImageWithCapInsets:edgeInsets];
    }
    return image;
}

the method : - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

has been deprecated in the iOS documentation but not in the framework, this mean that you can use it when you are running your app in a device whit iOS5.x without any problem, and user the new supported method with devices with iOS 6 or higher.

Upvotes: -2

Filip Radelic
Filip Radelic

Reputation: 26683

It's a new method introduced in iOS 6.0 and not supported on previous versions. If you want to make the code run on previous versions, you will have to check at runtime if UIImage instance responds to selector for that method and implement alternative if it doesn't.

if ([UIImage instancesRespondToSelector:@selector(resizableImageWithCapInsets:resizingMode:)]) {
    self.scrollViewImage.image = [[UIImage imageNamed:@"SysInfoBackBox"] resizableImageWithCapInsets:UIEdgeInsetsMake(40, 40, 40, 40) resizingMode:UIImageResizingModeStretch];
} else {
    // alternative
}

Upvotes: 11

surfrider
surfrider

Reputation: 1425

This function resizableImageWithCapInsets:resizingMode: don't work on ios 5.0 (only >=6.0), but resizableImageWithCapInsets: does, try to use it. Maybe a simple replacement can help you.

Upvotes: 4

Related Questions