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

标签 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)
        }

    })

最佳答案

保存图像:

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

关于ios - 如何使用 PHPhotoLibrary 将视频/图像写入保存的相册?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38021069/

相关文章:

ios - 无法将 UIViewController 类型的值转换为 PatternDetailViewController

ios - 禁用 PHPhotoLibrary 删除 Assets 中删除请求的确认?

ios - `PHAssetChangeRequest.creationRequestForAssetFromVideo(url:)` 无法在 iPhone SE 上播放高 FPS 视频

iphone - 无法从 C++ 文件访问对象属性 (Cocos2D/Box2d)

objective-c - 关于应用内购买

swift - 在 Swift 中获取 bool 的位模式

ios - 适用于 iOS 7.1 的 SKShapeNode 和 swift

swift - 无法处理 NSPhotoLibraryAddUsageDescription 的权限

ios - 多色暗影 swift

ios - 我怎样才能把角标(Badge)图标放在菜单栏的应用程序中?