resizableImageWithCapInsets:resizingMode: 在 iOS 5.1 上崩溃
问题描述:
我正在使用此代码正确拉伸图像,但是在 iOS 5.1 上它崩溃了.如果我从最后删除 resizingMode,它会工作,但图像会被平铺并且看起来很有趣.知道为什么会崩溃吗?
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?
谢谢
self.scrollViewImage.image = [[UIImage imageNamed:@"SysInfoBackBox"] resizableImageWithCapInsets:UIEdgeInsetsMake(40, 40, 40, 40) resizingMode:UIImageResizingModeStretch];
答
这是 iOS 6.0 中引入的新方法,以前的版本不支持.如果您想让代码在以前的版本上运行,则必须在运行时检查 UIImage 实例是否响应该方法的选择器,如果没有,则实施替代方案.
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
}