如何使用PHPhotoLibrary将视频/图像写入已保存的相册?

时间:2016-06-24 19:48:29

标签: ios swift phphotolibrary

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

    })

1 个答案:

答案 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
})