ios:colorWithPatternImage,拉伸图像全屏
我有一个视图,并希望通过UIImage为此视图设置backGroudColor。 代码如下:
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backImage.png"]];
问题是:背景图像比视图小。 如何将UIImage拉伸到完整的视图。 我使用UIImageView可以到达。
有人有好主意吗?
更新:
我无法上传一张图片。
像这样:backImge的大小是30 * 30,当我使用myview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backImage.png"]];
结果是myview的backGroup有很多'backImage.png'。 我的目标是有一个“backImage.png”strehe全面myview。
试试。
self.layer.contents = (id)[UIImage imageNamed:@"freshBackground.png"].CGImage;
据我所知,你不能直接调整背景的图案图像。 最简单的方法就是改变你的图像,以适应你的父视图的框架大小。 或者,您可以重新绘制您的图像,以适应您的父视图的大小。
UIView * yourView = [[UIView alloc] initWithFrame:CGRectMake(10.f, 100.f, 300.f, 100.f)]; UIImage * targetImage = [UIImage imageNamed:@"backImage.png"]; // redraw the image to fit |yourView|'s size UIGraphicsBeginImageContextWithOptions(yourView.frame.size, NO, 0.f); [targetImage drawInRect:CGRectMake(0.f, 0.f, yourView.frame.size.width, yourView.frame.size.height)]; UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [yourView setBackgroundColor:[UIColor colorWithPatternImage:resultImage]];
把world000的答案翻译成Swift:
self.layer.contents = UIImage(named: "freshBackground.png")?.CGImage
基于Kjuly的回答,如果你想让图像在中心正确插入而不是被拉伸,可以使用这种方法来获取新的图像:
#pragma mark - Image +(UIImage*)imageFitInCenterForSize:(CGSize)inSize forSourceImage:(UIImage*)inImage { // redraw the image to fit |yourView|'s size CGSize imageOriginalSize = inImage.size; UIImage *resultImage = nil; if (imageOriginalSize.width<=inSize.width && imageOriginalSize.height<=inSize.height) { UIGraphicsBeginImageContextWithOptions(inSize, NO, 0.f); [inImage drawInRect:CGRectMake((inSize.width-imageOriginalSize.width)/2.0, (inSize.height-imageOriginalSize.height)/2.0, imageOriginalSize.width, imageOriginalSize.height)]; resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } return resultImage; }
重载的变体也一样:
+(UIImage*)imageFitInCenterForSize:(CGSize)inSize forSourceImage:(UIImage*)inImage xOffset:(CGFloat)inXOffset yOffset:(CGFloat)inYOffset { // http://stackoverflow.com/questions/10629403/ios-colorwithpatternimage-stretch-image-full-screen // redraw the image to fit |yourView|'s size CGSize imageOriginalSize = inImage.size; UIImage *resultImage = nil; if (imageOriginalSize.width<=inSize.width && imageOriginalSize.height<=inSize.height) { UIGraphicsBeginImageContextWithOptions(inSize, NO, 0.f); [inImage drawInRect:CGRectMake((inSize.width-imageOriginalSize.width)/2.0+inXOffset, (inSize.height-imageOriginalSize.height)/2.0+inYOffset, imageOriginalSize.width, imageOriginalSize.height)]; resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } return resultImage; }
这里是基于以前的评论一个版本,首先缩放图像,然后作物比例方面填充。
func imageScaledToFillSize(size: CGSize, image: UIImage) -> UIImage { let aspect = image.size.width / image.size.height; UIGraphicsBeginImageContextWithOptions(size, false, 0) if (size.width / aspect <= size.height) { let resizedImg = self.imageScaledToSize(CGSize(width: size.height * aspect, height: size.height), image: image) resizedImg.drawInRect(CGRectMake((size.width - resizedImg.size.width)/2, 0, resizedImg.size.width, resizedImg.size.height)) } else { let resizedImg = self.imageScaledToSize(CGSize(width: size.width, height: size.width / aspect), image: image) resizedImg.drawInRect(CGRectMake(0, (size.height-resizedImg.size.height)/2, resizedImg.size.width, resizedImg.size.height)) } let imageR = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return imageR; } func imageScaledToSize(size: CGSize, image: UIImage) -> UIImage { UIGraphicsBeginImageContextWithOptions(size, false, 0.0); image.drawInRect(CGRectMake(0.0, 0.0, size.width, size.height)) let imageR = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext(); return imageR; }