0

大家好,我正在使用 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)
        }

    })
4

1 回答 1

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
})
于 2017-06-01T11:38:23.323 回答