Stretching a image in iOS using colorWithPatternImage

谁都会走 提交于 2019-12-08 04:39:08

问题


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

In iPad, it shows up like this:

In iPhone, it works fine.

The code for iPhone looks like this:

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

How to avoid the repetition? Must I use resizableImageWithCapInsets?

Need some guidance and suggestions..


回答1:


add the following method

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

now change your code as

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



回答2:


Yes you are correct use resizableImageWithCapInsets.

http://iosdevelopertips.com/user-interface/ios-5-uiimage-and-resizableimagewithcapinsets.html



来源:https://stackoverflow.com/questions/16705424/stretching-a-image-in-ios-using-colorwithpatternimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!