如何获得正确的区域来裁剪UIImage?

时间:2017-01-04 01:52:08

标签: ios objective-c uiimageview uiimage cgpath

我的目标c应用程序中有一个UIImageView。在此UIImageView我加载了一张图片,然后从相机中取出。我应用了一个区域来裁剪UIImage。该区域是CAShapeLayer的矩形。但我的问题是,当我得到包含在矩形中的框架时,这不是同一个位置,而是我的矩形......我得到一个错误的区域然后裁剪。

我使用此代码:

 CAShapeLayer *_outline = [CAShapeLayer new];
 UIImageView *vistaImagen;
 ...
UIGraphicsBeginImageContextWithOptions(_vistaImagen.frame.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextAddPath(context, _boundingBox.accessibilityPath.CGPath);

CGContextClip(context);
[_vistaImagen.layer renderInContext:context];
UIImage *iii = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

如果我要添加我的图层框架,可能会出现什么问题:_boundingBox.accessibilityPath.CGPath ??

谢谢!

1 个答案:

答案 0 :(得分:0)

好吧,花了几个小时后,我发现了问题。我正在创建一个规模较小的UIGraphicsBeginImageContextWithOptions ... 我需要使用scale to 1,因为我在UIImage中以实际比例获取图像,但我将比例初始化为0。

UIImageView *vistaImagen;
UIGraphicsBeginImageContextWithOptions(_vistaImagen.frame.size, NO, 1.0);