Commit 188911fa by Sarkizz

图片保存到相册添加授权判断和请求逻辑

parent 1a05b5a3
......@@ -86,34 +86,47 @@ extension UIImage {
/// 将图片写入到相册
/// - Parameter complate: 回调
public func saveToAlbum(_ complate: UIImageSaveToAlbumCompleteBlock?) {
var localId: String?
PHPhotoLibrary.shared().performChanges({
let rs = PHAssetChangeRequest.creationRequestForAsset(from: self)
let placeholder = rs.placeholderForCreatedAsset
localId = placeholder?.localIdentifier
}, completionHandler: { (isSuccessed, error) in
if isSuccessed {
if let id = localId {
let rs = PHAsset.fetchAssets(withLocalIdentifiers: [id], options: nil)
if let asset = rs.firstObject {
let options = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = { data in
return true
checkAuthorization({ status in
var localId: String?
PHPhotoLibrary.shared().performChanges({
let rs = PHAssetChangeRequest.creationRequestForAsset(from: self)
let placeholder = rs.placeholderForCreatedAsset
localId = placeholder?.localIdentifier
}, completionHandler: { (isSuccessed, error) in
if isSuccessed {
if let id = localId {
let rs = PHAsset.fetchAssets(withLocalIdentifiers: [id], options: nil)
if let asset = rs.firstObject {
let options = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = { data in
return true
}
asset.requestContentEditingInput(with: options, completionHandler: { (input, _) in
complate?(self, input?.fullSizeImageURL, nil)
})
} else {
complate?(self, nil, self.error(code: -2, msg: "获取图片资源失败"))
}
asset.requestContentEditingInput(with: options, completionHandler: { (input, _) in
complate?(self, input?.fullSizeImageURL, nil)
})
} else {
complate?(self, nil, self.error(code: -2, msg: "获取图片资源失败"))
complate?(self, nil, self.error(code: -1, msg: "保存图片缺失localIdentifier"))
}
} else {
complate?(self, nil, self.error(code: -1, msg: "保存图片缺失localIdentifier"))
complate?(self, nil, error)
}
} else {
complate?(self, nil, error)
}
})
})
}
/// 检查当前授权状态,未授权则调用授权接口
/// - Parameter completion: 回调
public func checkAuthorization(_ completion: @escaping (_ status: PHAuthorizationStatus) -> Void) {
let current = PHPhotoLibrary.authorizationStatus()
if current == .notDetermined {
PHPhotoLibrary.requestAuthorization(completion)
} else {
completion(current)
}
}
}
extension UIImage {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment