先將圖片鏈接地址轉換為data數據,然后轉換為image格式,最后調用
? ? funcdownPic(){
? ? ? ? ? let?urlString:String=typeImg//(Url:網絡地址)
? ? ? ? ? guardleturl? =URL(string:urlString)else{return}
? ? ? ? ? letdata =NSData(contentsOf: url)
? ? ? ? ? letimage :UIImage=UIImage.init(data: data!asData)!
? ? ? ? PHPhotoLibrary.shared().performChanges({
? ? ? ? ? ? letresult =PHAssetChangeRequest.creationRequestForAsset(from: image)
? ? ? ? letassetPlaceholder = result.placeholderForCreatedAsset
? ? ? ? //保存標志符
? ? ? ? self.localId= assetPlaceholder?.localIdentifier
? ? ? ? }) { (isSuccess:Bool, error:Error?)in
? ? ? ? ifisSuccess {
? ? ? ? print("保存成功!")
? ? ? ? //通過標志符獲取對應的資源
? ? ? ? letassetResult =PHAsset.fetchAssets(
? ? ? ? withLocalIdentifiers: [self.localId], options: nil)
? ? ? ? letasset = assetResult[0]
? ? ? ? let options = PHContentEditingInputRequestOptions()
? ? ? ? ? ? options.canHandleAdjustmentData= {(adjustmeta:PHAdjustmentData)
? ? ? ? ? ? ->Boolin
? ? ? ? ? ? returntrue
? ? ? ? ? ? }
? ? ? ? ? ? //獲取保存的圖片路徑
? ? ? ? ? ? asset.requestContentEditingInput(with: options, completionHandler: {
? ? ? ? ? ? (contentEditingInput:PHContentEditingInput?, info: [AnyHashable:Any])in
? ? ? ? ? ? print("地址:",contentEditingInput!.fullSizeImageURL!)
? ? ? ? ? ? ? ? self.imgUrl=String(describing: contentEditingInput!.fullSizeImageURL!)
? ? ? ? ? ? })
? ? ? ? ? ? //獲取保存的原圖
? ? ? ? ? ? PHImageManager.default().requestImage(for: asset,
? ? ? ? ? ? targetSize: PHImageManagerMaximumSize, contentMode: .aspectFit,
? ? ? ? ? ? options:nil,resultHandler: { (image, _:[AnyHashable:Any]?)in
? ? ? ? ? ? ? ? print("獲取原圖成功:\(String(describing: image))")
? ? ? ? ? ? })
? ? ? ? ? ? //獲取保存的縮略圖String(describing: )
? ? ? ? ? ? PHImageManager.default().requestImage(for: asset,
? ? ? ? ? ? targetSize: CGSize(width:100, height:100), contentMode: .aspectFit,
? ? ? ? ? ? options:nil,resultHandler: { (image, _:[AnyHashable:Any]?)in
? ? ? ? ? ? ? ? print("獲取縮略圖成功:\(String(describing: image))")
? ? ? ? ? ? })
? ? ? ? ? ? }else{
? ? ? ? ? ? print("保存失?。?, error!.localizedDescription)
? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? }