我有这样的图像:。在iPad中,当我使用这些代码放置它时:
[separatorLineView setFrame:CGRectMake(10, 0, 430, 3)];
separatorLineView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"divider.png"]];
在iPad中,它显示如下:
在iPhone中,它运行正常。
iPhone的代码如下所示:
[separatorLineView setFrame:CGRectMake(10, 0, 180, 3)];
separatorLineView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"divider.png"]];
如何避免重复?我必须使用resizableImageWithCapInsets吗?
需要一些指导和建议..
答案 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)
是的,你正确使用resizableImageWithCapInsets。
http://iosdevelopertips.com/user-interface/ios-5-uiimage-and-resizableimagewithcapinsets.html