iOS保存图片和视频到相册

1.保存图片到相册

OC

 //image是要保存的图片
- (void) saveImage:(UIImage *)image{

    if (image) {

        UIImageWriteToSavedPhotosAlbum(image, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);

    };

}
//保存完成后调用的方法
- (void) savedPhotoImage:(UIImage*)image didFinishSavingWithError: (NSError *)error contextInfo: (void *)contextInfo {
    if (error) {
        NSLog(@"保存图片出错%@", error.localizedDescription);
    }
    else {
        NSLog(@"保存图片成功");
    }
}

Swift

     func saveImage(image: UIImage) {
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
    }

     @objc func image(image: UIImage,didFinishSavingWithError: NSError?,contextInfo: AnyObject)  {
        if didFinishSavingWithError == nil {
           //保存成功
        }else{
            //保存失败
        }
    }

2.保存视频到相册

OC

//videoPath为视频下载到本地之后的本地路径
- (void)saveVideo:(NSString *)videoPath{

    if (_videoPath) {

            if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([_videoPath path])) {
                //保存相册核心代码
                UISaveVideoAtPathToSavedPhotosAlbum([_videoPath path], self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
            }
        
    }

}
//保存视频完成之后的回调
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        NSLog(@"保存视频失败%@", error.localizedDescription);
    }
    else {
        NSLog(@"保存视频成功");
    }
  
}

Swift

    func saeVideo(videoPath: String) {
        if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(videoPath) {
            UISaveVideoAtPathToSavedPhotosAlbum(videoPath, self, #selector(video(videoPath: didFinishSavingWithError:contextInfo:)), nil)
        }
    }
    
    @objc func video(videoPath: String,didFinishSavingWithError: NSError?,contextInfo: AnyObject)  {
        if didFinishSavingWithError == nil {
            //保存成功
        }else{
            //保存失败
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容