iphone - 将 UIView 的表示保存到文件

标签 iphone image uiview save

将 UIView 的表示保存到文件的最简单方法是什么?

我的解决方案是,

 UIGraphicsBeginImageContext(someView.frame.size);
 [someView drawRect:someView.frame];
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

 NSString* pathToCreate = @"sample.png";

 NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
 [imageData writeToFile:pathToCreate atomically:YES];

但这似乎很棘手,我认为必须有更有效的方法来做到这一点。

最佳答案

您还可以像这样使用图层:

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

关于iphone - 将 UIView 的表示保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3051630/

相关文章:

iphone - 获取 UIButton 所在的 View

Java Graphics Font - 如何确保文本保持在一行?

ios - 如何创建 UIView 的凹形

ios - 将 TableView 单元格添加到 View 中的 TableView

ios - View 卡在 MapView 后面

iPhone SDK : custom buttons in TableView

iphone - AVAudioRecorder 更改录制的音高

ios - 使用 iOS 上的应用程序录制屏幕视频

python - 如何从图像中读取标点符号,如 '/' 、 '_' 和 '\'

Java-创建瓦片 map gif