The Assets Library framework is deprecated as of iOS 9.0. Instead, use the Photos framework instead, which in iOS 8.0 and later provides more features and better performance for working with a user’s photo library.
不幸的是 Apple 并没有发布相关的使用指导文档,只有一个相关 Demo。使用的时候固然可以回头参考这个 Demo,但这样的效率不是很高,很多概念也容易忘记,所以这里做个简单的总结。
Photos 中有不少类,其中几个犹为关键。PHPhotoLibary 是我们操作 Photo Library 里面资源的入口对象,所有的操作都通过它完成。PHCollectionList 表示相册中的专题列表;PHAssetCollection 表示专题;PHAsset 表示资源,如 images, videos, and Live Photos.
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: image)
}, completionHandler: {success, error in
if !success { print("error creating asset: \(error)") }
})
创建一个资源到指定的专题
123456789
PHPhotoLibrary.shared().performChanges({
let creationRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
if let assetCollection = self.assetCollection {
let addAssetRequest = PHAssetCollectionChangeRequest(for: assetCollection)
addAssetRequest?.addAssets([creationRequest.placeholderForCreatedAsset!] as NSArray)
}
}, completionHandler: {success, error in
if !success { print("error creating asset: \(error)") }
})