先将图片链接地址转换为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)
}
}
}