任何人都可以向我解释如何在 iPhone OS 中将图像(均等和不均等地)拆分为多个部分。如果您提供示例代码,这对我很有帮助。
kmr
问问题
4660 次
2 回答
8
我已经改编了一些从Bill Dudney的核心动画书中得到的代码来完成这项任务:
- (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;
}
注意:要获得 CGImageRef:
CGImageRef anImage = [myUIImage CGImage];
于 2009-06-17T18:22:16.910 回答