如何将UIImage切割成碎片(比如披萨)并将每个碎片保存在阵列中?

时间:2015-08-06 12:12:29

标签: ios objective-c graphics uiimageview uiimage

将它保存在数组中没有问题,但我不知道如何剪切它。我已经找到了如何用矩形切割它,但我找不到像披萨一样切割它。

@implementation UIImage (Crop)

- (UIImage *)crop:(CGRect)rect {

    rect = CGRectMake(rect.origin.x*self.scale, 
                      rect.origin.y*self.scale, 
                      rect.size.width*self.scale, 
                      rect.size.height*self.scale);       

    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
    UIImage *result = [UIImage imageWithCGImage:imageRef 
                                          scale:self.scale 
                                    orientation:self.imageOrientation]; 
    CGImageRelease(imageRef);
    return result;
}

@end

在图片上,我试图展示我想做的事情。

之前:http://tinypic.com/r/287g1vq/8

之后:http://i58.tinypic.com/1zwnn93.jpg

1 个答案:

答案 0 :(得分:1)

So the core is that you'd like to cut ARC from your image. I won't comment the code here, as I wrote some comments in the code.

- (NSArray *)pizzaSlicesFromImage:(UIImage *)image withNumberOfSlices:(NSUInteger)numberOfSlices {
    if (!image) {
        NSLog(@"You need to pass an image to slice it! nil image argument occured."); // use cocoa lumberjack instead of logs

        return nil;
    } else if (numberOfSlices == 0) { // 0 slices? then you don't want the image at all
        return nil;
    } else if (numberOfSlices == 1) { // 1 slice? then it's whole image, just wrapped in array
        return @[image];
    }

    CGFloat fullCircle = 2 * M_PI;
    CGFloat singleSliceAngle = fullCircle / numberOfSlices;
    CGFloat previousSliceStartAngle = 0;

    NSMutableArray *mSlicesOfImage = [NSMutableArray new];
    for (NSUInteger i = 0; i < numberOfSlices; i++) {
        UIImage *sliceImage = [self imageFromAngle:previousSliceStartAngle toAngle:(previousSliceStartAngle + singleSliceAngle) fromImage:image];
        if (sliceImage) {
            [mSlicesOfImage addObject:sliceImage];
        }
        previousSliceStartAngle += singleSliceAngle;
    }

    // return non-mutable array
    return mSlicesOfImage.copy;
}

- (UIImage *)imageFromAngle:(CGFloat)fromAngle toAngle:(CGFloat)toAngle fromImage:(UIImage *)image {
    // firstly let's get proper size for the canvas
    CGRect imageRect = CGRectMake(0.f, 0.f, image.size.width, image.size.height);
    CGPoint centerPoint = CGPointMake(CGRectGetMidX(imageRect), CGRectGetMidY(imageRect));

    // start the drawing
    UIGraphicsBeginImageContext(imageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // we need to perform this to fix upside-down rotation
    CGContextTranslateCTM(context, 0, imageRect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // now slice an arc from the circle
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, centerPoint.x, centerPoint.y); // move to the center of drawing
    CGContextAddArc(context, centerPoint.x, centerPoint.y, MAX(CGRectGetWidth(imageRect), CGRectGetHeight(imageRect)) / 2.f, fromAngle, toAngle, 0); // draw the arc
    // ! if you want NOT to cut outer area to form the circle, just increase 4th value (radius) to cover "corners" of the rect drawn on the circle. I did it this way, because it looks like a pizza, like you wanted.
    CGContextClosePath(context);
    CGContextClip(context);

    // get the image, purge memory
    CGContextDrawImage(context, imageRect, image.CGImage);
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // this is one slice
    return resultImage;
}

That's not a full solution, as I don't have more time to complete it, however I'll try to finish it later. The only thing left is to cut the image so it is smaller, but I lack time to finish it, so it's a partial answer.

I hope it helps :) Hopefully will edit this answer later to add that missing cutting part.

BTW: I've created a module based on above implementation. You're welcome to check it out: https://github.com/natalia-osa/NORosettaView.