【发布时间】:2016-06-14 13:13:41
【问题描述】:
我有 UIScrollView 和 UIImageView 。您可以缩放它并裁剪可见部分。
这是我的 snap 代码:
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 透明部分的情况下拍摄图像。我试图猜测上下文和偏移坐标的不同大小,但没有成功,因为我显然不了解图像、滚动视图或/和 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
它可以防止图像占据不完整的滚动视图可见部分。
【问题讨论】:
标签: ios objective-c uiscrollview uiimageview cgcontext