OGeek|极客世界-中国程序员成长平台

标题: ios - 如何将 UIImage 切割成碎片(如披萨)并将每一个保存在数组中? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:37
标题: ios - 如何将 UIImage 切割成碎片(如披萨)并将每一个保存在数组中?

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

@implementation UIImage (Crop)

- (UIImage *)cropCGRect)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



Best Answer-推荐答案


所以核心是你想从你的图像中删除 ARC。这里就不评论代码了,因为我在代码中写了一些注释。

- (NSArray *)pizzaSlicesFromImageUIImage *)image withNumberOfSlicesNSUInteger)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 toAnglepreviousSliceStartAngle + singleSliceAngle) fromImage:image];
        if (sliceImage) {
            [mSlicesOfImage addObject:sliceImage];
        }
        previousSliceStartAngle += singleSliceAngle;
    }

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

- (UIImage *)imageFromAngleCGFloat)fromAngle toAngleCGFloat)toAngle fromImageUIImage *)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;
}

这不是一个完整的解决方案,因为我没有更多时间来完成它,但是我会尝试稍后完成它。唯一剩下的就是把图像剪得更小,但我没有时间完成它,所以这是一个部分答案。

我希望它有所帮助 希望稍后会编辑此答案以添加缺少的切割部分。

顺便说一句:我已经基于上述实现创建了一个模块。欢迎您查看:https://github.com/natalia-osa/NORosettaView .

关于ios - 如何将 UIImage 切割成碎片(如披萨)并将每一个保存在数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31855443/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4