保存圖片到本地和相冊

1.保存到iOS相冊需要引入QuartzCore.framework框架,具體代碼如下:.h文件#importUIImageView *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.保存圖片到本地

-(UIImage *) getImageFromURL:(NSString *)fileURL {

NSLog(@"執行圖片下載函數");

UIImage * result;

NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];

result = [UIImage imageWithData:data];

return result;

}

-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {

if ([[extension lowercaseString] isEqualToString:@"png"]) {

[UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];

} else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {

[UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];

} else {

//ALog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);

NSLog(@"文件后綴不認識");

}

}

保存

-(UIImage *) loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {

UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]];

return result;

}

NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSLog(@"保存路徑:%@",documentsDirectoryPath);

//Get Image From URL

UIImage * imageFromURL = [self getImageFromURL:@"http://file.duteba.com/phone/2009/04/5/ftGxL8kcUL.jpg"];

//Save Image to Directory

[self saveImage:imageFromURL withFileName:@"MyImage" ofType:@"jpg" inDirectory:documentsDirectoryPath];

//Load Image From Directory

UIImage * imageFromWeb = [self loadImage:@"MyImage" ofType:@"jpg" inDirectory:documentsDirectoryPath];

[img setImage:imageFromWeb];

//取得目錄下所有文件名

NSArray *file = [[[NSFileManager alloc] init] subpathsAtPath:documentsDirectoryPath];

//NSLog(@"%d",[file count]);

NSLog(@"%@",file);

顯示

NSString *documentsDirectory2 = [path2 objectAtIndex:0]; //path數組里貌似只有一個元素

//字符串拼接得到文件完整路徑

NSString *filestr = @"/MyImage.jpg";

NSString *newstr = [documentsDirectory2 stringByAppendingString:filestr];

NSLog(@"完整路徑是:%@",newstr);

NSData *dd = [NSData dataWithContentsOfFile:newstr];

[img222 setImage:[UIImage imageWithData:dd]];

另外,還可以將圖片的路徑保存在plist文件里,這樣可以避免過多的IO操作。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容