Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create UIImageView with portion of image file

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.

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.

like image 696
George Armhold Avatar asked Jun 26 '09 13:06

George Armhold


1 Answers

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);
like image 116
George Armhold Avatar answered Sep 22 '22 19:09

George Armhold