使用图像文件的一部分创建UIImageView

时间:2022-12-13 21:46:33

I'm subclassing UIImageView to create a tile-based application. Essentially, I am taking a single image file and breaking it up into pieces, and then assigning the pieces to my tiles (UIImageViews), so that they can be manipulated independently.

我正在继承UIImageView来创建基于tile的应用程序。基本上,我正在拍摄单个图像文件并将其分解成碎片,然后将碎片分配给我的切片(UIImageViews),以便可以单独操作它们。

What's the best way to grab a portion of an image and use that to draw a UIImageView? I thought about overriding drawRect and using CGAffineTransform, but it seems like there ought to be a simpler way to do this, perhaps by specifying a CGRect to the UIImage that is passed to the UIImageView, but I don't see an API for this.

获取图像的一部分并使用它来绘制UIImageView的最佳方法是什么?我想过覆盖drawRect并使用CGAffineTransform,但似乎应该有一个更简单的方法来做到这一点,可能是通过指定传递给UIImageView的UIImage的CGRect,但我没有看到这个的API。

1 个解决方案

#1


Here we go:

开始了:

UIImage* img = [UIImage imageNamed:@"myImage.jpg"];
CGRect imgFrame = CGRectMake(x, y, tileWidth, tileHeight);
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], imgFrame);
UIImage* subImage = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);

#1


Here we go:

开始了:

UIImage* img = [UIImage imageNamed:@"myImage.jpg"];
CGRect imgFrame = CGRectMake(x, y, tileWidth, tileHeight);
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], imgFrame);
UIImage* subImage = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);