2

我想将我正在使用的小图像(100 * 100)制作成大图像(1000 * 1000),但这是最糟糕的做法。

在这里,我在视图中添加了它,但我想在上下文中绘制它并保存它..

for(int j=0;j<5;j++)
      {
          for(int i=0;i<5;i++)
           {
         UIImageView* imgView = [[UIImageView alloc] initWithImage:  [UIImageimageNamed:@"images.jpeg"]];
          [imgView drawRect:CGRectMake(xCoord,yCoord,imgView.frame.size.width, imgView.frame.size.height)]; 
         yCoord =imgView.frame.size.height*j;

         [self.view addSubview:imgView];
          xCoord += imgView.frame.size.width;

          }
           yCoord=0;
           xCoord = 0.0;
       }

我想使用上下文 draw rect 来做到这一点。 CGContextDrawImage如何处理这个请帮助。在此先感谢....

4

3 回答 3

2

如果您希望将其绘制在 Context 上,请使用[image drawAsPatternInRect:rect]

编辑:

此方法将生成给定大小的新图像。重复绘制图像,直到填充整个上下文。

UIImage * TiledImage(UIImage *tile, CGSize size) {
    UIGraphicsBeginImageContext(size);
    [tile drawAsPatternInRect:CGRectMake(0,0, size.width, size.height)];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

在你的情况下,只需写:UIImage *bigImage = TiledImage([UIImageimageNamed:@"images.jpeg"], CGSizeMake(1000,1000));现在你有你的 1000x1000 像素图像。

于 2012-10-17T09:35:34.550 回答
0

这看起来就像您将获得 1000x1000UIView并将其backgroundColor属性设置为

[UIColor colorWithPatternImage:@"images.jpeg"];

如果你想在上下文中绘制:

CGContextRef context = UIGraphicsBeginImageContext(imageSize);
[patternImage drawAsPatternInRect:imageRect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
于 2012-10-17T09:30:45.877 回答
0

您可以通过这种方式将背景图案应用于任何视图:

UIImage *pattern = [UIImage imageNamed:@"yourimage-pattern.png"];
yourView.backgroundColor = [UIColor colorWithPatternImage:pattern];

我认为这比您目前使用的方法更容易。

编辑:

您仍然可以在上下文中呈现您的视图并截取它,如果这是您想要的:

UIGraphicsBeginImageContext(CGSizeMake(1000,1000)));
[yourView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

确保您正在使用QuartzCore lib并引用了头文件:#import <QuartzCore/QuartzCore.h>对于此代码片段。

于 2012-10-17T09:31:01.913 回答