43

我有一个视图,并希望通过 UIImage 为该视图设置背景颜色。代码为:

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

问题是:backImage'frame 比 view 小。如何拉伸 UIImage 以充满我的视野。我知道使用 UIImageView 可以达到。

有人有好主意吗?

更新:

我无法上传一张图片。

像这样:backImage的大小是30*30,我的视图大小是1024*700,当我使用myview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backImage.png"]];

结果是myview 的backGroup 有很多'backImage.png'。我的目标是拥有一个完整的“backImage.png”。

4

6 回答 6

123

试试。

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

迅速:

self.view.layer.contents = UIImage(named: "freshBackground")!.cgImage
于 2012-12-27T04:08:35.117 回答
25

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]];
于 2012-05-17T04:07:48.713 回答
3

world000 的答案翻译成 Swift:

self.layer.contents = UIImage(named: "freshBackground.png")?.CGImage
于 2015-08-31T20:05:44.103 回答
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
{
    // 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;
}
于 2013-02-21T06:58:06.200 回答
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];
于 2017-12-14T15:26:46.050 回答
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;
}
于 2015-10-17T12:22:56.527 回答