如何将由 CGContextRef 表示的位图图像保存到 iPhone 的硬盘上?

发布于 2024-09-14 18:39:31 字数 154 浏览 2 评论 0原文

我有一个小草图程序,我已经使用 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩世 2024-09-21 18:39:31

CGContext 有不同类型。您很可能拥有基于屏幕的上下文,但您需要 bitmapContext 或 pdfContext 来创建位图或 pdf 文件。

请参阅 UIKit 函数参考。应该感兴趣的函数是:

  • UIGraphicsBeginImageContext()
  • UIGraphicsGetImageFromCurrentImageContext()
  • UIGraphicsEndImageContext()
  • UIImageJPEGRepresentation()
  • UIImageWriteToSavedPhotosAlbum()
  • UIGraphicsBeginPDFContextToFile()
  • UIGraphicsEndPDFContext()

There are different types of CGContexts. You are most likely having a screen based context, but you would need either a bitmapContext or a pdfContext, to create a bitmap or a pdf file.

See the UIKit Functions Reference. Functions that should be of interest are:

  • UIGraphicsBeginImageContext()
  • UIGraphicsGetImageFromCurrentImageContext()
  • UIGraphicsEndImageContext()
  • UIImageJPEGRepresentation()
  • UIImageWriteToSavedPhotosAlbum()
  • UIGraphicsBeginPDFContextToFile()
  • UIGraphicsEndPDFContext()
傾城如夢未必闌珊 2024-09-21 18:39:31

这是我的做法...

保存

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];

Here's how I did it...

Saving

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

Retrieving

UIImage* image = [UIImage imageWithContentsOfFile:filePath];
CGImageRef imageRef = [image CGImage];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文