我正在使用JBCroppableView
来获取所选区域的Bezier路径。
//内置函数以获取所选区域的路径
UIBezierPath *path = [self getPath];
唯一的区别是JBCroppableView
屏蔽了所选区域,我正在尝试裁剪所选区域。
问题是没有裁剪精确选定的区域。一些随机外部区域也被裁剪,裁剪区域几乎是所选区域的1/4。 我正在使用以下代码,其中包含bezier路径并返回裁剪后的图像。
代码归功于https://stackoverflow.com/users/2976878/originaluser2
-(UIImage*) cropImage:(UIImage*)image withPath:(UIBezierPath*)path { // where the UIBezierPath is defined in the UIKit coordinate system (0,0) is top left
CGRect r = CGPathGetBoundingBox(path.CGPath); // the rect to draw our image in (minimum rect that the path occupies).
UIGraphicsBeginImageContextWithOptions(r.size, NO, image.scale); // begin image context, with transparency & the scale of the image.
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -r.origin.x, -r.origin.y); // translate context so that when we add the path, it starts at (0,0).
CGContextAddPath(ctx, path.CGPath); // add path.
CGContextClip(ctx); // clip any future drawing to the path region.
[image drawInRect:(CGRect){CGPointZero, image.size}]; // draw image
UIImage* i = UIGraphicsGetImageFromCurrentImageContext(); // get image from context
UIGraphicsEndImageContext(); // clean up and finish context
return i; // return image
}
这是选定的区域:
这是裁剪后的结果: