UIImageView *imageview = [[UIImageView alloc]initWithFrame:self.view.bounds];
imageview.image = [UIImage imageNamed:@"testImage.jpg"];
[self.view addSubview:imageview];
方法一:
/**
* C語言方法 把圖片保存到 相機(jī)膠卷
*
* @param imageview.image 將要保存的圖片
* @param self 指定執(zhí)行者(誰來執(zhí)行該方法)
* @param image:didFinishSavingWithError:contextInfo: 綁定觸發(fā)事件(蘋果規(guī)定的固定格式)
* @contextInfo 要往方法中傳入的參數(shù)(不傳就填nil)
*
*/
UIImageWriteToSavedPhotosAlbum(imageview.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
NSLog(@"保存圖片失敗");
} else {
NSLog(@"保存圖片成功");
}
}
方法二:
注意: 需要導(dǎo)入<Photos/Photos.h>框架
// 異步
[[PHPhotoLibrary sharedPhotoLibrary]performChanges:^{
/**
* 保存圖片到相機(jī)膠卷
*
*/
[PHAssetChangeRequest creationRequestForAssetFromImage:imageview.image];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (error) {
NSLog(@"保存圖片失敗");
} else {
NSLog(@"保存圖片成功");
}
}];
// 同步
NSError *error = nil;
[[PHPhotoLibrary sharedPhotoLibrary]performChangesAndWait:^{
/**
* 保存圖片到相機(jī)膠卷
*
*/
[PHAssetChangeRequest creationRequestForAssetFromImage:imageview.image];
} error:&error];
if (error) {
NSLog(@"保存圖片失敗");
} else {
NSLog(@"保存圖片成功");
}