美文网首页
保存图片到本地 相册

保存图片到本地 相册

作者: 艾希_可可 | 来源:发表于2018-11-06 17:21 被阅读9次

    情景一:保存网络图片到本地

     var result:NSData?
                    do{
                        if self.imageUrls.count>0 {
                            result = try Data(contentsOf: URL(string: self.imageUrls)!, options: Data.ReadingOptions.uncached) as NSData
                            let imag = UIImage(data: result! as Data, scale: 1)
                            PHPhotoLibrary.shared().performChanges({
                                PHAssetChangeRequest.creationRequestForAsset(from: imag!)
                            }) { (isSuccess, error) in
                                LSDKLog("\(isSuccess)----\(String(describing: error))")
                                if isSuccess {
                                    LSSVProgressHUD.LSShowSuccess("保存成功")
                                    self.shareView?.removeShareView()
                                } else {
                                    LSDKLog("error---->\(String(describing: error))")
                                    LSSVProgressHUD.LSShowSuccess("保存失败")
                                }
                                }
                        }else{
                            LSSVProgressHUD.LSShowSuccess("保存失败")
                        }
                    }catch{
                        print("请求图片地址错误")
                        LSSVProgressHUD.LSShowSuccess("保存失败")
                    }
    

    情景二:保存本地图片至本地
    方法二

    
    //MARK: Photos框架保存照片
    ///保存照片--> 建议使用 <Photos框架保存照片>
    func saveImage(image: UIImage) {
        PHPhotoLibrary.shared().performChanges({
            PHAssetChangeRequest.creationRequestForAsset(from: image)
        }) { (isSuccess, error) in
            print("\(isSuccess)----\(String(describing: error))")
            if isSuccess {
                MessateAlert.Use(titleStr: "保存成功")
            } else {
                print("error---->\(String(describing: error))")
                MessateAlert.Use(titleStr: "系统问题,保存失败")
            }
        }
    }
     
     
    ///保存图片,并从相册获取保存的照片
    func savePhotoAndGetphoto() {
        var localID: String!//标识符
        PHPhotoLibrary.shared().performChanges({
            let result = PHAssetChangeRequest.creationRequestForAsset(from: UIImage())
            let assetPlaceholder = result.placeholderForCreatedAsset
            //保存标识符
            localID = assetPlaceholder?.localIdentifier
        }) { (succ, error) in
            if succ {
                print("保存成功")
                //通过标识符获取对应的资源
                let assetResult = PHAsset.fetchAssets(withLocalIdentifiers: [localID], options: nil)
                let asset = assetResult[0]
                let options = PHContentEditingInputRequestOptions()
                options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
                    return true
                }
                //获取保存的图片路径
                asset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput: PHContentEditingInput?, info: [AnyHashable : Any]) in
                    let file = contentEditingInput!.fullSizeImageURL!
                    print("地址:\(file)")
                })
                
                //获取保存的原图
                PHImageManager.default().requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: PHImageContentMode.aspectFit, options: nil, resultHandler: { (image, handle: [AnyHashable : Any]?) in
                    print("获取原图成功:\(String(describing: image))")
                })
                
                //获取保存的缩略图
                PHImageManager.default().requestImage(for: asset, targetSize: CGSize.init(width: 100, height: 100), contentMode: PHImageContentMode.aspectFit, options: nil, resultHandler: { (image, handle: [AnyHashable : Any]?) in
                    print("获取缩略图成功:\(String(describing: image))")
                })
            } else {
                print("保存失败-->\(error!.localizedDescription)")
            }
        }
    
    }
    

    方法一

    ///保存图片 --> UIImageWriteToSavedPhotosAlbum
    func saveImageToAlbum(image:UIImage) {
        print("图片--->\(image)")
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
    }
    @objc func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafeRawPointer) {
        if let e = error as NSError? {
            print(e)
        } else {
            MessateAlert.Use(titleStr: "保存成功")
        }
    
    

    相关文章

      网友评论

          本文标题:保存图片到本地 相册

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