ResizableImageWithCapInsets: resizingMode: crash on iOS 5.1

I use this code to stretch the image correctly, however on iOS 5.1 it will work. If I remove resizeMode from the end, it works, but the image is then laid out and looks funny. Any ideas why it is crashing?

thanks

self.scrollViewImage.image = [[UIImage imageNamed:@"SysInfoBackBox"] resizableImageWithCapInsets:UIEdgeInsetsMake(40, 40, 40, 40) resizingMode:UIImageResizingModeStretch]; 
+4
source share
3 answers

This is a new method introduced in iOS 6.0 and not supported in previous versions. If you want the code to run in previous versions, you will need to check it at run time if the UIImage instance responds to a selector for this method and implements an alternative if it is not.

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

This function resizableImageWithCapInsets:resizingMode: does not work on ios 5.0 (only> = 6.0), but resizableImageWithCapInsets: does, will try to use it. Perhaps a simple replacement might help you.

+4
source

I replayed another question that should be related to your problem fooobar.com/questions/1434075 / ...

but if you like, I can answer this question:

this only happened with devices with iOS5.x, resizing the UIImageView, which creates the UIImage created in this way:

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

this is probably an iOS bug fixed in iOS6.x

If your case resizes the image using mirror criteria, you can use this method:

create a UIImage category 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; } 

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

deprecated in the iOS documentation, but not in the structure, this means that you can use it when launching the application on a device with iOS5.x without any problems, and the user is a new supported method with devices with iOS 6 or higher.

-2
source

Source: https://habr.com/ru/post/1434074/


All Articles