0
我正在使用下面的裁剪方法来裁剪Uiimage坐在UIImageView,然后坐在UIScrollView。问题cropImage方法在UIScrollView中的图像产生错误的图像
-(UIImage *)cropImage:(UIImage *)image
{
float scale = 1.0f/_scrollView.zoomScale;
NSLog(@"Oh and heres that zoomScale: %f", _scrollView.zoomScale);
CGRect visibleRect;
visibleRect.origin.x = _scrollView.contentOffset.x * scale;
visibleRect.origin.y = _scrollView.contentOffset.y * scale;
visibleRect.size.width = _scrollView.bounds.size.width * scale;
visibleRect.size.height = _scrollView.bounds.size.height * scale;
NSLog(@"Oh and here's that CGRect: %f", visibleRect.origin.x);
NSLog(@"Oh and here's that CGRect: %f", visibleRect.origin.y);
NSLog(@"Oh and here's that CGRect: %f", visibleRect.size.width);
NSLog(@"Oh and here's that CGRect: %f", visibleRect.size.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], visibleRect);
UIImage *croppedImage = [[UIImage alloc] initWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
我需要将图像裁剪为CGSize(321,115)。在剪切图像并看到打印结果后,我可以看到visibleRect是(0,0,321,115) - 它应该是这样的,然后UIImage的宽度为321,高度为115。由于某些原因,图像似乎完全放大得太远了(该方法将原始图像的一小部分裁剪为321x115的大小)。
为什么此方法不能正确裁剪我的图像?
- 作为一个方面说明:当我调用这个方法时,我打电话给_croppedImage = [self cropImage:_imageView.image];
,它将自定义UIView类的UIImage属性设置为裁剪后的图像。
这段代码如何考虑我的UIScrollView zoomScale?我必须考虑UIScrollView的scaleFactor,否则这不会裁剪所选区域 – Chisx
这意味着您只想裁剪选定的区域。对 ?如果是,请检查已编辑的答案 –
是的,我几乎工作使用http://stackoverflow.com/questions/7760191/cropping-a-uiimage-according-to-the-viewport-inside-a-uiscrollview但有问题与该代码..只有稍微关闭。是的,只有选定的区域..基本上是缩放区域或出现在UIScrollView中的区域。这很接近,但那不是我正在寻找的内容,我在这里分享的链接基本上是我在寻找的内容,但它不能完全正常工作。 – Chisx