2010-08-15 62 views

回答

3

這裏是我是如何做到的?

保存

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

-1的代碼泄漏imageRef和形象。 – 2010-08-16 01:37:08

+1

我沒有包含發佈聲明,但他們在那裏。 – BeachRunnerFred 2010-08-16 02:13:37

+3

+1代碼snippits不會泄漏內存 – 2010-08-23 20:57:24

3

有不同類型的CGContexts。你很可能有一個基於屏幕的上下文,但你需要一個bitmapContext或pdfContext來創建一個位圖或PDF文件。

查看UIKit Functions Reference。這應該是感興趣的功能是:

  • UIGraphicsBeginImageContext()
  • UIGraphicsGetImageFromCurrentImageContext()
  • UIGraphicsEndImageContext()
  • UIImageJPEGRepresentation()
  • UIImageWriteToSavedPhotosAlbum()
  • UIGraphicsBeginPDFContextToFile()
  • UIGraphicsEndPDFContext()
相關問題