iOS:colorWithPatternImage,如何将图片拉伸至全屏?

43

我有一个视图,想通过UIImage为该视图设置背景颜色。 代码如下:

self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backImage.png"]];

问题是:backImage的尺寸小于视图。如何将UIImage拉伸到填满整个视图。

我知道可以使用UIImageView解决。

有没有什么好的想法?

更新:

我不能上传一个图片。

就像这样:backImge的尺寸为30*30,而我的视图尺寸为1024*700, 当我使用myview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backImage.png"]];

结果是我的视图背景上有许多"backImage.png"。

我的目标是只有一个"backImage.png"可以拉伸到填满我的视图。


你能展示一些图片吗?这样我们可以更好地了解问题。 - rishi
可能这是你的问题 - http://stackoverflow.com/questions/8651276/background-color-colorwithpatternimage-not-filling-entire-view-for-ipad - rishi
1
Objective-C,Xcode 10.14,self.layer.contents = (__bridge id _Nullable)([UIImage imageNamed: @"freshBackground"].CGImage); - dengApro
6个回答

125

来试试看。

self.layer.contents = (id)[UIImage imageNamed:@"freshBackground.png"].CGImage;

Swift:

self.view.layer.contents = UIImage(named: "freshBackground")!.cgImage

这里有一个教程,它解释了这个问题:http://www.raywenderlich.com/2502/calayers-tutorial-for-ios-introduction-to-calayers-tutorial - SirNod
完美的解决方案。运行良好。 - Dilshan
10
2015年,至今仍是最佳解决方案。 - Şafak Gezer
非常棒的答案,但如果需要使用UIColor来改变alpha组件(半透明图像)的话,这个方法可能不太合适。 - Valentin Mercier
3
太棒了伙计。对于Swift代码:self.view.layer.contents = UIImage.init(named:"freshBackground")!.CGImage翻译为:self.view.layer.contents = UIImage(named:"freshBackground")!.cgImage - arshu

25
据我所知,你不能直接调整背景图案图片的大小。最简单的方法就是将你的图片更改为适合父视图框架大小的尺寸。或者你可以重新绘制你的图片来适应父视图的大小。
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]];

在实现这个方法时我遇到了麻烦,直到我意识到我的UIView在xib中的尺寸被设置为自由形式。将UIView的大小更改为 "推断" 就解决了我的问题。 - ja928

3

world000的回答已翻译为Swift:

self.layer.contents = UIImage(named: "freshBackground.png")?.CGImage

2
我用以下方法来做到:
  1. 在特定的界限内绘制图片(使其缩放)
  2. 使用结果图像作为模式。

代码:

UIGraphicsBeginImageContext(self.window.frame.size);
[[UIImage imageNamed:@"background"] drawInRect:self.window.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.window.backgroundColor = [UIColor colorWithPatternImage:image];

2

根据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
{
    // https://dev59.com/e2gv5IYBdhLWcg3wZ_--
    // 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;
}

这对我非常有帮助。 - CRDave

0

根据之前的评论,这里提供一个版本,先按比例缩放图像,然后进行纵横比填充裁剪。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接