将图片保存到相机胶卷

   UIImageView *imageview = [[UIImageView     alloc]initWithFrame:self.view.bounds];
   imageview.image = [UIImage imageNamed:@"testImage.jpg"];
   [self.view addSubview:imageview];

方法一:
/**
* C语言方法 把图片保存到 相机胶卷
*
* @param imageview.image 将要保存的图片
* @param self 指定执行者(谁来执行该方法)
* @param image:didFinishSavingWithError:contextInfo: 绑定触发事件(苹果规定的固定格式)
* @contextInfo 要往方法中传入的参数(不传就填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(@"保存图片成功");
  }
}

方法二:
注意: 需要导入框架

// 异步
[[PHPhotoLibrary sharedPhotoLibrary]performChanges:^{
    /**
     *  保存图片到相机胶卷
     *
     */
    [PHAssetChangeRequest creationRequestForAssetFromImage:imageview.image];
    
} completionHandler:^(BOOL success, NSError * _Nullable error) {
    if (error) {
        NSLog(@"保存图片失败");
    } else {
        NSLog(@"保存图片成功");
    }
}];

// 同步
NSError *error = nil;
[[PHPhotoLibrary sharedPhotoLibrary]performChangesAndWait:^{
    /**
     *  保存图片到相机胶卷
     *
     */
    [PHAssetChangeRequest creationRequestForAssetFromImage:imageview.image];
    
} error:&error];
if (error) {
    NSLog(@"保存图片失败");
} else {
    NSLog(@"保存图片成功");
}

你可能感兴趣的:(将图片保存到相机胶卷)