iphone - 如何将绘制的内容保存在 CGContext 中

标签 iphone cocoa-touch uiview

我已经绘制到 UIView 的 CGContext 中。

- (void)drawRect:(CGRect)rect { 
    [self drawInContext:UIGraphicsGetCurrentContext()]  
}

我想将绘制的内容保存到 png 文件中。

有没有简单的解决办法?

编辑:基于下面的建议 - 这是我到目前为止所拥有的......

-(void)createImage {
    NSString* outFile = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.png"];
    DLog(@"creating image file at %@", outFile);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    NSData *imageData = UIImagePNGRepresentation(image);
    [imageData writeToFile:outFile 
                atomically:NO];
}

- (void)drawRect:(CGRect)rect { 
    [self drawInContext:UIGraphicsGetCurrentContext()]; 
    [self createImage];
}

最佳答案

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:@"image.png"];

关于iphone - 如何将绘制的内容保存在 CGContext 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2568421/

相关文章:

iphone - 通过 iPhone SDK 获取 LinkedIn 中特定用户的群组列表

objective-c - 用于多对多对多关系的 NSPredicate 过滤器

iphone - UIToolbar 项目消失 - ios6 中的奇怪错误

iphone - UIScrollview 滚动时调用 super View layoutSubviews?

iphone - 获取当前UIView的ID objective-c iphone

iphone - View 内的 View (iOS、绘图属性)

iPhone #ifdef 指令未按预期工作

iphone - 启动图像未在已安装的 ios web 应用程序上更新

ios - 无法阻止搜索栏显示取消按钮

ios - 使用 Swift 以编程方式将 UIView 中的标签和图像居中对齐