创建的UIImageView与图像文件的一部分(create UIImageView with po

2019-06-24 06:03发布

我继承UIImageView创建一个基于区块的应用。 从本质上讲,我以一个图像文件,并分解成片,然后分配件我的瓷砖( UIImageViews ),这样他们可以独立操作。

什么是抓住一个图像的一部分,并用它来绘制的最好方式UIImageView ? 我想到了重写drawRect和使用CGAffineTransform ,但它好像有应该是要做到这一点,也许通过指定一个简单的方法CGRectUIImage传递到UIImageView ,但我没有看到这个的API。

Answer 1:

开始了:

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


文章来源: create UIImageView with portion of image file