使用 colorWithPatternImage 在 iOS 中拉伸图像 [英] Stretching a image in iOS using colorWithPatternImage

查看:40
本文介绍了使用 colorWithPatternImage 在 iOS 中拉伸图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的图像:.在 iPad 中,当我使用这些代码放置它时:

I have a image like this: . In iPad, when I place it with these code:

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

在 iPad 中,它显示如下:

In iPad, it shows up like this:

在 iPhone 中,它运行良好.

In iPhone, it works fine.

iPhone 的代码如下所示:

The code for iPhone looks like this:

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

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

How to avoid the repetition? Must I use resizableImageWithCapInsets?

需要一些指导和建议..

Need some guidance and suggestions..

推荐答案

添加如下方法

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

这篇关于使用 colorWithPatternImage 在 iOS 中拉伸图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆