【发布时间】:2012-07-03 19:12:36
【问题描述】:
我让用户从相机中捕捉图像或从库中挑选一张。
我在UIImageView 中显示此图像。
用户现在可以在边界框内缩放和定位图像,就像在 allowsEditing 设置为 YES 时使用 UIImagePickerController 所做的一样。
当用户对结果感到满意并点击完成时,我想生成一个裁剪后的UIImage。
使用CGImageCreateWithImageInRect 时会出现问题,因为这没有考虑缩放。像这样将转换应用于 imageView:
CGAffineTransform transform = CGAffineTransformScale(self.imageView.transform, newScale, newScale);
[self.imageView setTransform:transform];
使用手势识别器。
我假设正在发生的事情是; UIImageView 被缩放和移动,然后将 UIViewContentModeScaleAspectFit 应用到 UIImage 是持有的,当我要求它裁剪图像时,它就是这样做的 - 不考虑缩放定位。我这么认为的原因是,如果我不缩放或移动图像,而是直接点击完成,裁剪就可以了。
我像这样裁剪图像:
- (UIImage *)cropImage:(UIImage*) img toRect:(CGRect)rect {
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale>1.0) {
rect = CGRectMake(rect.origin.x*scale , rect.origin.y*scale, rect.size.width*scale, rect.size.height*scale);
}
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], rect);
UIImage *result = [UIImage imageWithCGImage:imageRef scale:self.imageView.image.scale orientation:self.imageView.image.imageOrientation];
// UIImage *result = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return result;
}
从我的主视图的子视图(方形叠加框,如 UIImagePickerController 中)传入一个cropRect。主 UIView 有一个可缩放的 UIImageView 和一个显示裁剪矩形的 UIView。
如何获得“所见即所得”的裁剪以及我必须考虑哪些因素。或者如果我应该以不同的方式实现层次结构或缩放,也许会提出建议。
【问题讨论】:
-
我也遇到了同样的问题,你有解决办法吗?
-
对此有什么解决办法吗?有同样的问题。
标签: ios objective-c uiimageview uiimage crop