用数组保存没问题,就是不知道怎么切。我找到了如何将它切成矩形,但找不到如何像比萨饼一样切割它。
@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
在图片上我试图展示我想做什么。
最佳答案
所以核心是你想从图像中剪切 ARC。我不会在这里注释代码,因为我在代码中写了一些注释。
- (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;
}
这不是一个完整的解决方案,因为我没有更多时间来完成它,但我会尝试稍后完成它。唯一剩下的就是把图像剪小一点,但我没有时间完成它,所以这是一个部分答案。
希望对您有所帮助 :) 希望稍后编辑此答案以添加缺少的剪辑部分。
顺便说一句:我已经基于上述实现创建了一个模块。欢迎您查看:https://github.com/natalia-osa/NORosettaView .
关于ios - 如何将 UIImage 切成碎片(如披萨)并将每个碎片保存在数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31855443/