gpt4 book ai didi

objective-c - 如何从 iOS 上的 GUI 元素合成图像?

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

我需要通过组合一些视觉元素来形成图像并将其保存在磁盘上。问题是:如何“截图” View 的某个区域?可能是一个不可见的 View ,因此可以在不被注意的情况下执行该过程?

最佳答案

此代码片段展示了如何将 View 渲染到 UIImage:

UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

这个片段展示了如何将 UIImage 保存为 JPEG 或 PNG:

NSString  *pngPath = [NSHomeDirectory() 
stringByAppendingPathComponent:@"Documents/Test.png"];
NSString *jpgPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents/Test.jpg"];
[UIImageJPEGRepresentation(viewImage, 1.0) writeToFile:jpgPath atomically:YES];
[UIImagePNGRepresentation(viewImage) writeToFile:pngPath atomically:YES];

关于objective-c - 如何从 iOS 上的 GUI 元素合成图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7932981/

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