ResizebleMageWithCapInsets:ResizengMode:在iOS 5.1上崩溃

ResizebleMageWithCapInsets:ResizengMode:在iOS 5.1上崩溃,ios,xcode,uiimage,Ios,Xcode,Uiimage,我使用这段代码来正确拉伸图像,但是在iOS 5.1上它崩溃了。 如果我从末尾删除resizingMode,它会工作,但是图像会平铺,看起来很有趣。 知道它为什么会崩溃吗 谢谢 self.scrollViewImage.image = [[UIImage imageNamed:@"SysInfoBackBox"] resizableImageWithCapInsets:UIEdgeInsetsMake(40, 40, 40, 40) resizingMode:UIImageResizingMode

我使用这段代码来正确拉伸图像,但是在iOS 5.1上它崩溃了。 如果我从末尾删除resizingMode,它会工作,但是图像会平铺,看起来很有趣。 知道它为什么会崩溃吗

谢谢

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

这是iOS 6.0中引入的一种新方法,在以前的版本中不受支持。如果要使代码在以前的版本上运行,则必须在运行时检查UIImage实例是否响应该方法的选择器,如果不响应,则实现alternative

if ([UIImage instancesRespondToSelector:@selector(resizableImageWithCapInsets:resizingMode:)]) {
    self.scrollViewImage.image = [[UIImage imageNamed:@"SysInfoBackBox"] resizableImageWithCapInsets:UIEdgeInsetsMake(40, 40, 40, 40) resizingMode:UIImageResizingModeStretch];
} else {
    // alternative
}
此函数
resizebleimagewithcapinsets:resizengmode:
不要在ios 5.0上工作(仅>=6.0),但是
resizebleimagewithcapinsets:

是的,试着使用它。也许一个简单的替换可以帮助你。

我重复了另一个应该与你的问题相关的问题

但如果你愿意,我可以在这里给出答案:

这种情况仅发生在iOS5.x调整UIImageView大小的设备上,该UIImageView以以下方式显示创建的UIImage:

    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(topCapHeight, leftCapWidth, topCapHeight, leftCapWidth);
    image = [originalImage resizableImageWithCapInsets:edgeInsets];
这可能是iOS6.x中修复的一个iOS错误

如果您的案例使用镜像条件调整图像大小,您可以使用以下方法:

    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(topCapHeight, leftCapWidth, topCapHeight, leftCapWidth);
    image = [originalImage resizableImageWithCapInsets:edgeInsets];
创建UIImage的类别并添加此实例方法:

- (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;
}
-(UIImage*)大小可调整的图像,带leftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
{
UIImage*image=nil;
浮动osVersion=[[UIDevice currentDevice]系统版本]浮动值];
如果(osVersion<6.0){
图像=[具有leftCapWidth:leftCapWidth topCapHeight:topCapHeight的自拉伸图像];
}否则{
UIEdgeInsets edgeInsets=UIEdgeInsetsMake(顶帽高度、左帽宽度、顶帽高度、左帽宽度);
图像=[带capinsets的自调整大小图像:edgeInsets];
}
返回图像;
}
方法:
-(UIImage*)可拉伸图像,带leftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight


在iOS文档中已被弃用,但在框架中未被弃用,这意味着您可以在iOS 5.x设备上运行应用程序时使用它,而不会出现任何问题,并且在iOS 6或更高版本的设备上使用新支持的方法。

Ahh确实如此。谢谢我没有意识到它会崩溃。我希望编译器会发出警告,因为我的构建版本是iOS 5Build版本?您是指部署目标还是SDK版本?部署目标仅设置您希望应用程序尝试运行的最低版本,确保向后兼容性取决于您。如果您将SDK版本设置为iOS 5.1或更高版本,则会出现错误,而不是警告。是部署目标抱歉。谢谢你的提示。顺便问一下,iOS 5是否有其他方法?虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效。如果您以这种方式进行更改,则此帖子应作为以下内容的副本关闭: