訪問權(quán)限 兩個 記得info.plist中添加
Privacy - Photo Library Additions Usage Description
Privacy - Photo Library Usage Description
//swift方法一: 使用系統(tǒng)的回調(diào)方法
let image = UIImage()
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
func image(image: UIImage, didFinishSavingWithError: NSError?, contextInfo: AnyObject){
if PHPhotoLibrary.authorizationStatus() == .notDetermined {
PHPhotoLibrary.requestAuthorization({ (status) in
if status == .authorized {
print("點(diǎn)同意")
} else if status == .denied || status == .restricted{
print("點(diǎn)拒絕")
}
})
} else {
print("無權(quán)限")
return
}
if didFinishSavingWithError != nil{
print("保存失敗")
}else{
print("保存成功")
}
}
注:selector(image(image:didFinishSavingWithError:contextInfo:))斟览,這個方法是系統(tǒng)的方法抛丽,oc的方法如下圖所示:
oc的方法.png
swift:方法二: 使用Photo庫中的方法
@objc private func demoBtnClick(){
let image = UIImage()
savePhoto(image: image)
}
private func savePhoto(image: UIImage){
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: image)
}){ (isSuccess: Bool, error: Error?) in
if isSuccess == true{
print("保存成功!")
}else{
print("保存失斏Ω:", error!.localizedDescription)
}
}
}