0

I have a UIImage that is 1024 x 1 pixels. This image has to stretch to fill a UIImageView that is 1024 x 50 pixels.

This image is loaded initially on a NSArray and then I do this:

 UIImage *image = nil;

    if ([UIImage respondsToSelector:@selector(resizableImageWithCapInsets:resizingMode:)]) { // ios >= 6.0
        image = [[myImages objectAtIndex:0] resizableImageWithCapInsets:UIEdgeInsetsZero
resizingMode:UIImageResizingModeStretch];
    } else {
        image = [[myImages objectAtIndex:0] stretchableImageWithLeftCapWidth:0.0f topCapHeight:0.0f];
    }

this works great on iOS 6 but not on iOS 5. Why?

I mean, the image stretches beautifully and fills the UIImageView on iOS 6 but not on 5.

The UIImageView is set with these parameters:

UIImageView myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,1024,50)];
[myImageView setContentMode:UIViewContentModeScaleToFill | UIViewContentModeRedraw];
[myImageView setImage:image];
6
  • resizableImageWithCapInsets:... is an instance method, so you cannot use respondsToSelector: on the class itself, you'd need an instance for that (otherwise you're checking if the UIImage class responds to the selector). You could simply replace it with instancesRespondToSelector: here.
    – omz
    May 4, 2013 at 17:13
  • ah, ok, but it was working fine, for some reason.
    – Duck
    May 4, 2013 at 17:19
  • Btw, it doesn't really make sense to create a stretchable image with zero edge insets / cap widths. You could just use the original image there (the default is basically to stretch the entire image, these methods are only meant to be used when you want the borders of your image not to stretch). I suspect that the behavior for this "edge case" changed from iOS 5 to 6.
    – omz
    May 4, 2013 at 17:54
  • Ok I agree but that was the only way I managed to make the image stretch on iOS 6 but iOS 5 is not liking it.
    – Duck
    May 4, 2013 at 17:58
  • adding the image to the UIImageView without using this stretchable stuff makes no difference. The image continues to not stretch. Man, this is a nightmare.
    – Duck
    May 4, 2013 at 18:06

2 Answers 2

0

Call resizableImageWithCapInsets: with a good edge insets value instead of your UIEdgeInsetsZero. To do so, work out the width and height of your view. Then:

image = [myView resizableImageWithCapInsets:UIEdgeInsetsMake(
             height/2.0-1, width/2.0-1, height/2.0-1, width/2.0-1];
0
[[myImages objectAtIndex:0] stretchableImageWithLeftCapWidth:0.0f topCapHeight:0.0f]]

This is deprecated method in iOS 5. please go through this

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImage_Class/DeprecationAppendix/AppendixADeprecatedAPI.html

It may help you.

8
  • you have not read my code. Despite apple saying that resizableImageWithCapsInside is a iOS 5 API, when this code of mine runs on iOS 5, the line respondToSelector returns returns NO, that is resizableImageWithCapsInside does not work for iOS 5. If I force it to run the app crashes.
    – Duck
    May 4, 2013 at 17:10
  • by the way, just using resizableImageWithCapInsets: does not solve the problem.
    – Duck
    May 4, 2013 at 17:17
  • @RubberDuck go through this link stackoverflow.com/questions/12414844/… resizableImageWithCapsInside method is introduced in iOS 6 May 4, 2013 at 17:19
  • Well when your app crashes what error you are getting in console. May 4, 2013 at 17:20
  • ok, I was using resizableImageWithCapInsets:resizingMode: on iOS 5, now I have corrected that to just resizableImageWithCapInsets: and the crash is gone but the image do not stretches. Using resizableImageWithCapInsets:resizingMode: on iOS6 makes the image stretch.
    – Duck
    May 4, 2013 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.