书签我的问题是完全一样的。
我已经在我的裁剪视图中移动并让它正确显示图像的一部分,但是处理滚动视图缩放是一项正在进行的工作。目前我在 UIScrollview 中有一个 imageview 和普通 UIView 分层,然后在滚动视图之外,在更高级别是我的自定义裁剪 UIView。我实现了 touches 方法来处理拖动它,还实现了 drawRect:
- (void)drawRect:(CGRect)rect {
// Drawing code
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);