How can I save a Bitmap Image, represented by a CGContextRef, to the iPhone's hard drive?

亡梦爱人 提交于 2019-12-03 22:28:39

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()

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!