ios - 如何将 UIImage 切割成碎片(如披萨)并将每一个保存在数组中?
<p><p>把它保存在数组中没有问题,但我不知道如何切割它。我已经找到了如何将它切成矩形,但我找不到如何像披萨一样切割它。</p>
<pre><code>@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(, rect);
UIImage *result = [UIImage imageWithCGImage:imageRef
scale:self.scale
orientation:self.imageOrientation];
CGImageRelease(imageRef);
return result;
}
@end
</code></pre>
<p>我试图在图片上展示我想做的事情。</p>
<p>之前:<a href="http://tinypic.com/r/287g1vq/8" rel="noreferrer noopener nofollow">http://tinypic.com/r/287g1vq/8</a> </p>
<p>之后:<a href="http://i58.tinypic.com/1zwnn93.jpg" rel="noreferrer noopener nofollow">http://i58.tinypic.com/1zwnn93.jpg</a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>所以核心是你想从你的图像中删除 ARC。这里就不评论代码了,因为我在代码中写了一些注释。</p>
<pre><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 @;
}
CGFloat fullCircle = 2 * M_PI;
CGFloat singleSliceAngle = fullCircle / numberOfSlices;
CGFloat previousSliceStartAngle = 0;
NSMutableArray *mSlicesOfImage = ;
for (NSUInteger i = 0; i < numberOfSlices; i++) {
UIImage *sliceImage = ;
if (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;
}
</code></pre>
<p>这不是一个完整的解决方案,因为我没有更多时间来完成它,但是我会尝试稍后完成它。唯一剩下的就是把图像剪得更小,但我没有时间完成它,所以这是一个部分答案。</p>
<p>我希望它有所帮助 :) 希望稍后会编辑此答案以添加缺少的切割部分。</p>
<p>顺便说一句:我已经基于上述实现创建了一个模块。欢迎您查看:<a href="https://github.com/natalia-osa/NORosettaView" rel="noreferrer noopener nofollow">https://github.com/natalia-osa/NORosettaView</a> .</p></p>
<p style="font-size: 20px;">关于ios - 如何将 UIImage 切割成碎片(如披萨)并将每一个保存在数组中?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31855443/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31855443/
</a>
</p>
页:
[1]