How can i split an image in to multiple parts?
cocoa-touch, image-manipulation, iphone, uiimage
Solution
I have adapted some code I got out of Bill Dudney's Core Animation Book to accomplish this task:
- (NSArray*)splitImageIntoRects:(CGImageRef)anImage{
CGSize imageSize = CGSizeMake(CGImageGetWidth(anImage), CGImageGetHeight(anImage));
NSMutableArray *splitLayers = [NSMutableArray array];
kXSlices = 3;
kYSlices = 3;
for(int x = 0;x < kXSlices;x++) {
for(int y = 0;y < kYSlices;y++) {
CGRect frame = CGRectMake((imageSize.width / kXSlices) * x,
(imageSize.height / kYSlices) * y,
(imageSize.width / kXSlices),
(imageSize.height / kYSlices));
CALayer *layer = [CALayer layer];
layer.frame = frame;
CGImageRef subimage = CGImageCreateWithImageInRect(drawnImage, frame);
layer.contents = (id)subimage;
CFRelease(subimage);
[splitLayers addObject:layer];
}
}
return splitLayers;
}
Note: To get an CGImageRef:
CGImageRef anImage = [myUIImage CGImage];
Problem
Can any body explain me how an image can be (equally and unequally) split in to multiple parts in iPhone OS. It is very helpful to me if you provide sample code.