UIImage在iOS 5上没有延伸

时间:2013-05-04 16:26:41

标签: iphone ios objective-c ipad cocoa-touch

我有一个1024 x 1像素的UIImage。此图像必须拉伸以填充1024 x 50像素的UIImageView。

此图像最初加载到NSArray上,然后执行此操作:

 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];
    }

这适用于iOS 6,但不适用于iOS 5.为什么?

我的意思是,图像拉伸得漂亮,并在iOS 6上填充UIImageView但不在5上。

使用以下参数设置UIImageView:

UIImageView myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,1024,50)];
[myImageView setContentMode:UIViewContentModeScaleToFill | UIViewContentModeRedraw];
[myImageView setImage:image];

2 个答案:

答案 0 :(得分:0)

[[myImages objectAtIndex:0] stretchableImageWithLeftCapWidth:0.0f topCapHeight:0.0f]]

这是iOS 5中不推荐使用的方法。请仔细阅读

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

它可能对你有帮助。

答案 1 :(得分:0)

使用良好的边缘插入值而不是resizableImageWithCapInsets:来调用UIEdgeInsetsZero。为此,请计算视图的宽度和高度。然后:

image = [myView resizableImageWithCapInsets:UIEdgeInsetsMake(
             height/2.0-1, width/2.0-1, height/2.0-1, width/2.0-1];