【发布时间】:2019-06-08 08:33:07
【问题描述】:
总结: 有没有办法控制用户设备上应用程序创建的相册的排序顺序?我想要我创建的专辑按日期排序,但它按下载顺序排序。
详情:
我正在使用下面的函数save() 将 UIImage 保存到用户 iPhone 上的相册中。它工作得很好,但是保存副本时图像会丢失其原始创建时间。为了解决这个问题,我在将图像上传到服务器时阅读了创建时间。这样我只需在使用 assetChangeRequest.creationDate = storedImageDate 创建资产时将其分配给图像
PHPhotoLibrary.shared().performChanges({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
let assetPlaceholder = assetChangeRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(for: assetCollection)
assetChangeRequest.creationDate = storedImageDate
let assetEnumeration:NSArray = [assetPlaceholder!]
albumChangeRequest!.addAssets(assetEnumeration)
}, completionHandler: { success, error in
if error != nil {
print("\(error?.localizedDescription ?? "")")
}
})
以上效果很好,当我查看新创建的图像时,它在创建的相册中,图像上的图像日期正确,并且在相机胶卷中查看的图像按预期的图像创建日期排序。转到我的应用程序为图像创建的相册时会出现问题。那里的图像有图像日期,但它们是按下载顺序而不是图像排序的。我还注意到,当我长按相册中的图像时,它可以让我移动顺序。
我在创建相册时做错了吗?我可以将其设置为按图像日期排序吗?也许有一种方法可以更改我的专辑的排序顺序?有没有办法通过专辑获取循环以按日期排序?
这是将图像保存到不按图像创建日期排序的已创建相册中的完整功能。
func save(image:UIImage, albumName:String) {
var albumFound = false
var assetCollection: PHAssetCollection!
var assetCollectionPlaceholder: PHObjectPlaceholder!
// https://stackoverflow.com/questions/27008641/save-images-with-phimagemanager-to-custom-album
// Check if the Album exists and get collection
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
let collection : PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
if let _: AnyObject = collection.firstObject {
albumFound = true
assetCollection = collection.firstObject
} else {
//If not found - Then create a new album
PHPhotoLibrary.shared().performChanges({
let createAlbumRequest : PHAssetCollectionChangeRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: albumName)
assetCollectionPlaceholder = createAlbumRequest.placeholderForCreatedAssetCollection
}, completionHandler: { success, error in
albumFound = success
if (success) {
let collectionFetchResult = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [assetCollectionPlaceholder.localIdentifier], options: nil)
print(collectionFetchResult)
assetCollection = collectionFetchResult.firstObject
}
})
}
if albumFound {
PHPhotoLibrary.shared().performChanges({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
let assetPlaceholder = assetChangeRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(for: assetCollection)
assetChangeRequest.creationDate = storedImageDate
let assetEnumeration:NSArray = [assetPlaceholder!]
albumChangeRequest!.addAssets(assetEnumeration)
}, completionHandler: { success, error in
if error != nil {
print("\(error?.localizedDescription ?? "")")
}
})
}
}
【问题讨论】:
-
我在 PHAssetCollectionChangeRequest 的文档中找到了 moveAsset,但我不完全确定如何使用它来正确按日期对专辑进行排序。感觉很笨拙,但我怀疑我可以读取专辑中的资产数组,创建当前索引/正确索引的数组,然后循环将它们移动到新位置,如果那不是资源占用? developer.apple.com/documentation/photokit/…
标签: swift uiimage phasset phphotolibrary