大家好我正在使用ALAssetsLibrary将视频/图像写入相册。 但XCode告诉我ALAssetsLibrary已弃用,我需要使用PHPhotoLibrary,这一切都很好,但没有关于如何进行此转换的示例或文档。
有人可以告诉我如何更改此代码以使用PHPHotoLibrary吗?
let data:NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
let image:UIImage = UIImage( data: data)!
let library:ALAssetsLibrary = ALAssetsLibrary()
let orientation: ALAssetOrientation = ALAssetOrientation(rawValue: image.imageOrientation.rawValue)!
library.writeImageToSavedPhotosAlbum(image.CGImage, orientation: orientation, completionBlock: nil)
和视频:
ALAssetsLibrary().writeVideoAtPathToSavedPhotosAlbum(outputFileURL, completionBlock: {
(assetURL:NSURL!, error:NSError!) in
if error != nil{
print(error)
}
do {
try NSFileManager.defaultManager().removeItemAtURL(outputFileURL)
} catch _ {
}
if backgroundRecordId != UIBackgroundTaskInvalid {
UIApplication.sharedApplication().endBackgroundTask(backgroundRecordId)
}
})
答案 0 :(得分:0)
保存图片:
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: image)
}, completionHandler: { (success, error) in
// completion callback
})
视频:
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: mediaUrl as URL)
}, completionHandler: { (success, error) in
// completion callback
})