如何将由CGContextRef表示的位图图像保存到iPhone的硬盘? [英] How can I save a Bitmap Image, represented by a CGContextRef, to the iPhone's hard drive?

查看:122
本文介绍了如何将由CGContextRef表示的位图图像保存到iPhone的硬盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小草图程序,我已经设法使用Quartz2D,但我是iOS平台的新手,我很难弄清楚如何让用户保存他们的草图。我有一个包含草图的CGContextRef,如何保存它以便以后可以检索?一旦保存,我该如何找回它?

I have a little sketch program that I've managed to throw together using Quartz2D, but I'm very new to the iOS platform and I'm having trouble figuring out how to allow the user to save their sketch. I have a CGContextRef that contains the sketch, how can I save it so that it can later be retrieved? Once it's saved, how can I retrieve it?

非常感谢您的帮助!我现在要继续读这个......

Thanks so much in advance for your help! I'm going to continue reading up on this now...

推荐答案

这就是我做的...

保存

CGImageRef imageRef = CGBitmapContextCreateImage(bitmapContext);
UIImage* image = [[UIImage alloc] initWithCGImage:imageRef];
NSData* imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filePath atomically:YES];

检索

UIImage* image = [UIImage imageWithContentsOfFile:filePath];
CGImageRef imageRef = [image CGImage];

这篇关于如何将由CGContextRef表示的位图图像保存到iPhone的硬盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆