gpt4 book ai didi

ios - CALayer renderInContext 绘制空白图像

转载 作者:行者123 更新时间:2023-11-28 19:06:19 29 4
gpt4 key购买 nike

此代码有效:

UIGraphicsBeginImageContextWithOptions(aRect.size, NO, 0.0);
[self.view drawViewHierarchyInRect:aRect afterScreenUpdates:YES];
anImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

但我需要支持 iOS 5 和 6。我的谷歌搜索表明这段代码应该可以工作:

UIGraphicsBeginImageContextWithOptions(aRect.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
anImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

但是图像是空白的。如何在 iOS 5 和 6 中拍摄 View 快照?

最佳答案

解决方案是缩放 View 以适应图形上下文的边界。我发现的大多数示例都假设源 View 和目标上下文的大小相同。我使用的图形上下文比被快照的 View 小得多,它实际上只是剪切了 View 的一个透明角。

UIGraphicsBeginImageContextWithOptions(aRect.size, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat scale = CGRectGetWidth(aRect) / CGRectGetWidth(self.view.bounds);
CGContextScaleCTM(ctx, scale, scale);
[self.view.layer renderInContext:ctx];
anImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

关于ios - CALayer renderInContext 绘制空白图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20133646/

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