我在 UIScrollView 中有一张图片,可以滚动和缩放.
I have an image in an UIScrollView, that can be scrolled and zoomed.
当用户按下按钮时,我希望代码从我用 CGRect 指定的区域内的 UIScrollView 的任何部分创建图像.
When the user presses a button, I want the code to create an image from whatever part of the UIScrollView is inside an area I specify with a CGRect.
我见过裁剪 UIImages 的代码,但我无法调整它来为视图做同样的事情,因为它使用 CGContextDrawImage.
I've seen code to crop UIImages, but I can't adapt it to do the same for a view, because it uses CGContextDrawImage.
有什么想法吗?
干杯,安德烈
我已经搞定了.
这是我的解决方案,基于网络上的几个不同的解决方案:
Here's my solution, based on a few different ones from the web:
- (UIImage *)imageByCropping:(UIScrollView *)imageToCrop toRect:(CGRect)rect
{
CGSize pageSize = rect.size;
UIGraphicsBeginImageContext(pageSize);
CGContextRef resizedContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(resizedContext, -imageToCrop.contentOffset.x, -imageToCrop.contentOffset.y);
[imageToCrop.layer renderInContext:resizedContext];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
您使用以下方式调用:
CGRect clippedRect = CGRectMake(0, 0, 320, 300);
picture.image = [self imageByCropping:myScrollView toRect:clippedRect];
这篇关于如何从 UIView/UIScrollView 创建图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
从 imageview 点击手势获取索引或标签值get index or tag value from imageview tap gesture(从 imageview 点击手势获取索引或标签值)
清除分页 UIScrollView 中的自动旋转转换Clean autorotation transitions in a paging UIScrollView(清除分页 UIScrollView 中的自动旋转转换)
如何使用计时器自动滚动 UIScrollView?How to auto scroll UIScrollView using timer?(如何使用计时器自动滚动 UIScrollView?)
以编程方式强制 UIScrollView 停止滚动,以便与多个Programmatically force a UIScrollView to stop scrolling, for sharing a table view with multiple data sources(以编程方式强制 UIScrollView 停止滚
Xcode &Swift - 在 UIScrollView 内检测 UIView 的用Xcode amp; Swift - Detecting user touch of UIView inside of UIScrollView(Xcode amp;Swift - 在 UIScrollView 内检测 UIView 的用户触摸)
UIScrollView 中的 contentOffset 有什么作用?What does contentOffset do in a UIScrollView?(UIScrollView 中的 contentOffset 有什么作用?)