gpt4 book ai didi

iphone - 如何从 UIView/UIScrollView 创建图像

转载 作者:行者123 更新时间:2023-12-03 18:31:27 24 4
gpt4 key购买 nike

我在 UIScrollView 中有一个图像,可以滚动和缩放。

当用户按下按钮时,我希望代码从 UIScrollView 位于我用 CGRect 指定的区域内的任何部分创建图像。

我见过裁剪 UIImages 的代码,但我无法对其进行调整以对 View 执行相同的操作,因为它使用 CGContextDrawImage。

有什么想法吗?

干杯,安德烈

最佳答案

我已经成功得到它了。

这是我的解决方案,基于网络上的一些不同的解决方案:

- (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];

关于iphone - 如何从 UIView/UIScrollView 创建图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2500915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com