將UIImage保存到iOS照片庫和對應程序沙盒中

1.保存到iOS照片庫需要引入QuartzCore.framework框架,具體代碼如下:
.h文件
#import <QuartzCore/QuartzCore.h>
UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
.m文件
UIGraphicsBeginImageContext(imageView.bounds.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *temp = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(temp, nil, nil, nil);

2.保存到對應的沙盒目錄中,具體代碼如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"sms.gif"]];   // 保存文件的名稱
BOOL result = [UIImagePNGRepresentation(imageView.image)writeToFile: filePath    atomically:YES]; // 保存成功會返回YES 
發佈了25 篇原創文章 · 獲贊 4 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章