如何从 UIView/UIScrollView 创建图像
- 作者: 这尼玛美女裸图极品
- 来源: 51数据库
- 2022-10-20
问题描述
我在 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];
推荐阅读
热点文章
检查拆分键盘
0
带有“上一个"的工具栏和“下一个"用于键盘输入AccessoryView
0
Activity 启动时显示软键盘
0
UIWebView 键盘 - 摆脱“上一个/下一个/完成"酒吧
0
在 iOS7 中边缘滑动时,使键盘与 UIView 同步动画
0
我的 iOS 应用程序中的键盘在 iPhone 6 上太高了.如何在 XCode 中调整键盘的分辨率?
0
android:inputType="textEmailAddress";- '@' 键和 '.com' 键?
0
禁用 iPhone 中键盘的方向
0
Android 2.3 模拟器上的印地语键盘问题
0
keyDown 没有被调用
0
