如何将图像分割成多个部分?

时间:2023-01-22 07:21:31

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.

任何机构都可以向我解释一下图像在iPhone OS中如何(同样且不平等地)分成多个部分。如果您提供示例代码,对我非常有帮助。

2 个解决方案

#1


I have adapted some code I got out of Bill Dudney's Core Animation Book to accomplish this task:

我已经调整了一些我从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; 
}

Note: To get an CGImageRef:

注意:获取CGImageRef:

CGImageRef anImage = [myUIImage CGImage];

#2


If you just want a region defined by an NSRect then this code should do it. If you need more complex regions (non-rectangular) then Apple's Cropped Image example includes code for cropping to a Bezier curve (see -croppedImage in CroppingImageView.m).

如果您只想要一个由NSRect定义的区域,那么此代码应该这样做。如果您需要更复杂的区域(非矩形),那么Apple的裁剪图像示例包含用于裁剪到贝塞尔曲线的代码(请参阅CroppingImageView.m中的-croppedImage)。

#1


I have adapted some code I got out of Bill Dudney's Core Animation Book to accomplish this task:

我已经调整了一些我从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; 
}

Note: To get an CGImageRef:

注意:获取CGImageRef:

CGImageRef anImage = [myUIImage CGImage];

#2


If you just want a region defined by an NSRect then this code should do it. If you need more complex regions (non-rectangular) then Apple's Cropped Image example includes code for cropping to a Bezier curve (see -croppedImage in CroppingImageView.m).

如果您只想要一个由NSRect定义的区域,那么此代码应该这样做。如果您需要更复杂的区域(非矩形),那么Apple的裁剪图像示例包含用于裁剪到贝塞尔曲线的代码(请参阅CroppingImageView.m中的-croppedImage)。