美文网首页
保存图片、视频到相册

保存图片、视频到相册

作者: 彧哥哥 | 来源:发表于2020-07-23 00:03 被阅读0次

    iOS 11 新增了“ Privacy - Photo Library Additions Usage Description ”权限

    而 iOS10 及以前(换句话说也就是 XCODE8 及以前打包的 APP )的照片权限(“ Privacy - Photo Library Usage Description ”)根本没有“永不”,“仅添加照片”,“读取和写入”的三个区分。

    Info.plist 加入权限

    <key>NSPhotoLibraryAddUsageDescription</key>
    <string>App需要您的同意,才能保存相册</string>
    

    保存图片
    swift 4.0

        //MARK:- save image
        func WM_FUNC_saveImage(_ image:UIImage) -> Void {
            UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
        }
    @objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
        if let error = error {
            // we got back an error!
            let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default))
            present(ac, animated: true)
        } else {
            let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default))
            present(ac, animated: true)
        }
    }
    

    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(@"save error%@", error.localizedDescription);
        }
        else {
            NSLog(@"save success");
        }
    }
    

    保存视频
    swift4.0

     //MARK:- save video
        func WM_FUNC_saveVideo(_ urlStr:String) -> Void {
            UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, #selector(videoSaveStatus(_:didFinishSavingWithError:contextInfo:)), nil)
        }
        @objc func videoSaveStatus(_ urlstr: String, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer){
           if error != nil {
           //error
           }else{
           //success
           }
        }
    

    oc

    //videoPath为视频下载到本地之后的本地路径 URL.path
    - (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(@"save error%@", error.localizedDescription);
        }
        else {
            NSLog(@"save success");
        }
    }
    

    相关文章

      网友评论

          本文标题:保存图片、视频到相册

          本文链接:https://www.haomeiwen.com/subject/xznxlktx.html