Create UIImageView with part of image file

I am subclassing UIImageViewto create a tile based application. Essentially, I take one image file and break it into pieces, and then assign the parts to my tiles ( UIImageViews) so that they can be manipulated independently.

What is the best way to capture part of an image and use it for drawing UIImageView? I was thinking about overriding drawRectand using CGAffineTransform, but it seems that there should be an easier way to do this, perhaps by pointing CGRectto UIImagewhich is passed in UIImageView, but I do not see the API for this.

+5
source share
1 answer

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

All Articles