iphone – 如何从UIView / UIScrollView创建图像
发布时间:2020-12-14 19:49:27 所属栏目:百科 来源:网络整理
导读:我有一个UIScrollView中的图像,可以滚动和缩放. 当用户按下按钮时,我希望代码可以使用CGRect指定的区域内的UIScrollView的任何部分创建图像. 我已经看到了修改UIImage的代码,但是由于使用了CGContextDrawImage,因此我无法适应它对视图的一致性. 有什么想法吗
我有一个UIScrollView中的图像,可以滚动和缩放.
当用户按下按钮时,我希望代码可以使用CGRect指定的区域内的UIScrollView的任何部分创建图像. 我已经看到了修改UIImage的代码,但是由于使用了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,320,300); picture.image = [self imageByCropping:myScrollView toRect:clippedRect]; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |