PHAsset相冊中的資源
PHAssetCollection 相冊 是PHAsset的一個(gè)集合
PHPhotoLibrary 圖庫代表整個(gè)相冊
可以執(zhí)行一些增刪等操作
PHImageManager 訪問PHAsset里的真正的圖片或者視頻
PHAssetChangeRequest 是改變類蓬豁,需要PHPhotoLibrary去執(zhí)行
PHCachingImageManager 取資源時(shí)的緩存設(shè)置
func createAlbum() {
//查找又沒有創(chuàng)建"我創(chuàng)建的"這個(gè)相冊篷帅,沒有的話再創(chuàng)建 ,遍歷的是用戶自定義的相冊不是智能相冊
let result = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)
result.enumerateObjects { (collect, index, stop) in
if collect.localizedTitle == "我創(chuàng)建的" {
stop.pointee = true
}
if index == result.count-1 {
PHPhotoLibrary.shared().performChanges {
PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: "我創(chuàng)建的")
} completionHandler: { (flag, error) in
print(error)
}
}
}
}
func addPhoto() {
let result = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)
result.enumerateObjects { (collect, index, stop) in
if collect.localizedTitle == "我創(chuàng)建的" {
stop.pointee = true
PHPhotoLibrary.shared().performChanges {
let image = UIImage(named: "home_banner")
PHAssetChangeRequest.creationRequestForAsset(from: image!)
let request = PHAssetCollectionChangeRequest(for: collect)
let placeholder = request?.placeholderForCreatedAssetCollection
request?.addAssets([placeholder] as NSFastEnumeration)
} completionHandler: { (flag, error) in
print(error)
}
}
}
}
func deletePhoto() {
let result = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: nil)
result.enumerateObjects { (collect, index, stop) in
if collect.localizedTitle == "我創(chuàng)建的" {
stop.pointee = true
let result = PHAsset.fetchAssets(in: collect, options: nil)
result.enumerateObjects { (asset, index, stop) in
PHImageManager.default().requestImage(for: asset, targetSize: CGSize(width: asset.pixelWidth, height: asset.pixelHeight), contentMode: .aspectFill, options: nil) { (image, dict) in
}
PHPhotoLibrary.shared().performChanges {
if index == collect.accessibilityElementCount()-1 {
stop.pointee = true
PHAssetCollectionChangeRequest.deleteAssetCollections([asset] as NSFastEnumeration)
}
} completionHandler: { (flag, error) in
print(error)
}
}
}
}
}