ios: colorWithPatternImage ,拉伸图片全屏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10629403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ios: colorWithPatternImage , stretch image full screen
提问by lee_zhou
I have one view , and want set backGroudColor for this view by UIImage. The code as :
我有一个视图,并希望通过 UIImage 为该视图设置 backGroudColor。代码如下:
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backImage.png"]];
the problem is: the backImage'frame is small than the view. how to stretch UIImage to full my view. I konw use UIImageView can reach.
问题是:backImage'frame 比视图小。如何将 UIImage 拉伸到我的视野。我知道使用 UIImageView 可以达到。
someone have good idea?
有人有好主意吗?
update:
更新:
I can't upload one image.
我无法上传一张图片。
Like this:backImge's size is 30*30, and my view's size is 1024*700,
when i use myview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backImage.png"]];
像这样:当我使用时,backImge 的大小是 30*30,而我的视图大小是 1024*700 myview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backImage.png"]];
The result is myview's backGroup has many 'backImage.png'. my goal is have one 'backImage.png' streth full myview.
结果是 myview 的 backGroup 有很多 'backImage.png'。我的目标是有一个 'backImage.png' streth full myview。
回答by world000
Have a try.
试试。
self.layer.contents = (id)[UIImage imageNamed:@"freshBackground.png"].CGImage;
self.layer.contents = (id)[UIImage imageNamed:@"freshBackground.png"].CGImage;
Swift:
迅速:
self.view.layer.contents = UIImage(named: "freshBackground")!.cgImage
回答by Kjuly
As far as I know, you cannot resize the background's pattern image directly. The easiest way is just to change your image to fit your parent view's frame size. Or you can redraw your image to fit the size of your parent view.
据我所知,您不能直接调整背景图案图像的大小。最简单的方法就是改变你的形象,以适应你的父视图的帧大小。或者您可以重新绘制图像以适合父视图的大小。
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]];
回答by Connor Krupp
world000's answer translated to Swift:
world000 的答案翻译成 Swift:
self.layer.contents = UIImage(named: "freshBackground.png")?.CGImage
回答by Michael
I use the following method to:
我使用以下方法:
- draw the image within the specific bounds (which makes it scale)
- use the resulting image as a pattern.
- 在特定范围内绘制图像(使其缩放)
- 使用生成的图像作为图案。
Code:
代码:
UIGraphicsBeginImageContext(self.window.frame.size);
[[UIImage imageNamed:@"background"] drawInRect:self.window.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.window.backgroundColor = [UIColor colorWithPatternImage:image];
回答by Raj Pawan Gumdal
And based on Kjuly's answer, if you want the image to be inset properly at the center rather than being stretched, use this method to get the new image:
根据 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;
}
The same with overloaded variant:
与重载变体相同:
+(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;
}
回答by ceed
Here's based on previous comments a version that scales the image first, then crops to proportionally aspect fill.
这是基于之前的评论的一个版本,它首先缩放图像,然后裁剪成比例填充。
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;
}