收藏我的问题,它完全相同。
我已经成功移动了我的裁剪视图,并正确显示了图像的部分,但处理滚动视图缩放仍在进行中。目前,我在UIScrollview中叠加了一个imageview和普通UIView,然后在scrollview之外,在更高的级别上是我的自定义裁剪UIView。我实现了触摸方法来处理拖动它的位置,并且也使用drawRect:方法绘制它。
- (void)drawRect:(CGRect)rect {
if( self.image == nil )
return;
CGPoint offset = scrollView.contentOffset;
clipRect = CGRectOffset(self.frame, offset.x, offset.y);
UIImage *croppedImage = [image croppedImage:clipRect];
CGContextRef ctx = UIGraphicsGetCurrentContext();
[croppedImage drawAtPoint:CGPointMake(0, 0)];
}
注意:我的裁剪视图不是UIImageView的子类,它只是一个UIView,并引用其他视图中底层图像。
"croppedImage"是UIImage的一个分类,非常简单:
@implementation UIImage (Resize)
- (UIImage *)croppedImage:(CGRect)bounds {
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
...
@end
我已经实现了一些UIScrollViewDelegate方法,并通过滚动活动传递,以便我可以保持我的裁剪视图同步。缩放也被传递了,但是如上所述,还没有达到预期的效果。
另一个注意事项:我认为没有必要浪费时间每次生成一个新的UIImage来裁剪区域,因为应该可以使用相同的方法。
CGImageRef imageRef = CGImageCreateWithImageInRect([yourImage CGImage], bounds);
and
CGContextDrawImage(ctx, clipRect, imageRef);