使用colorWithPatternImage在iOS中拉伸图像

时间:2013-05-23 03:55:58

标签: ios uiimage stretching

我有这样的图像:enter image description here。在iPad中,当我使用这些代码放置它时:

[separatorLineView setFrame:CGRectMake(10, 0, 430, 3)];
separatorLineView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"divider.png"]];

在iPad中,它显示如下:

enter image description here

在iPhone中,它运行正常。

iPhone的代码如下所示:

[separatorLineView setFrame:CGRectMake(10, 0, 180, 3)];
    separatorLineView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"divider.png"]];

如何避免重复?我必须使用resizableImageWithCapInsets吗?

需要一些指导和建议..

2 个答案:

答案 0 :(得分:7)

添加以下方法

-(UIImage *)imageResize :(UIImage*)img andResizeTo:(CGSize)newSize
{
    CGFloat scale = [[UIScreen mainScreen]scale];

    //UIGraphicsBeginImageContext(newSize);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, scale);
    [img drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

现在将代码更改为

[separatorLineView setFrame:CGRectMake(10, 0, 180, 3)];
    separatorLineView.backgroundColor = [UIColor colorWithPatternImage:[self imageResize :[UIImage imageNamed:@"divider.png"] andResizeTo:separatorLineView.frame.size]];

答案 1 :(得分:-1)