我的UIScrollView上有UIImageView。您可以缩放它并裁剪可见部分。捕捉UIScrollView的可见部分后图像的透明部分
这里是我的卡代码:
UIImage *visibleScrollImage = nil;
UIGraphicsBeginImageContextWithOptions(self.scrollView.bounds.size, YES, [UIScreen mainScreen].scale);
{
CGPoint offset = self.scrollView.contentOffset;
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -offset.x, -offset.y);
[self.scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
visibleScrollImage = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
eivc.editImage = visibleScrollImage;
当图像不走的UIScrollView的所有可见的部分,我不知道如何捕捉图像,而UIImageView的透明部分。我试图用上下文和偏移坐标的不同尺寸来猜测,但它没有奏效,因为我显然不理解有关图像,scrollview或/和CGContext的内容。
这里有一些例子,我设置backgroundColor红色的可见性。
编辑:
现在我做了一半的解决方案 我设置imageView.frame这样
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
CGFloat initialImageHeight = result.size.height;
CGFloat initialImageWidth = result.size.width;
CGFloat height = self.scrollView.bounds.size.height;
CGFloat width = self.scrollView.bounds.size.width;
CGFloat frameHeight = height;
CGFloat frameWidth = width;
if ((initialImageHeight/initialImageWidth) > (height/width)) {
frameHeight = width * initialImageHeight/initialImageWidth;
} else if ((initialImageHeight/initialImageWidth) < (height/width)) {
frameWidth = height * initialImageWidth/initialImageHeight;
}
self.imageView.frame = CGRectMake(0, 0, frameWidth, frameHeight);
self.imageView.image = result;
和我做了minimumZoomScale = 1.0
它阻止图像采取不完整的滚动视图可见部分。
我得到了imageview的范围缩放时改变部分。只是冷静地想出如何调整权利。 所以我试过你的解决方案,这里的交易它不会削减正确的矩形,它不考虑滚动视图偏移量,这里是例子 这里是2个不同的可见部分 [1](http:// imgur的.com/tJw6m8W) [2](http://imgur.com/BCFPHGS) 在这两种情况下我得到这个 [结果](http://imgur.com/6hAW4CW) ,这里是相同的 [ 1](http://imgur.com/BAV2t1y) [2](http://imgur.com/gta91ZR) 结果 [结果](http://imgur.com/BQToolS) 另外我不'了解“类型”变量的用途。 –