Swift iOS:-如何将 .livePhotos 排除在 PHFetchOptions 之外?
Swift iOS: -How to exclude .livePhotos from inclusion in the PHFetchOptions?
在使用 UIImagePicker
时遵循 @matt 的 code,我可以防止用户在选择图像后选择 .livePhoto
使用:
let asset = info[UIImagePickerControllerPHAsset] as? PHAsset
if asset?.playbackStyle == .livePhoto {
// alert user this photo isn't a possibility
}
使用 PHFetchOptions 时,如何防止它们显示而不是在 enumerateObjects 回调中过滤掉它们?
fileprivate func fetchPhotos() {
let fetchOptions = PHFetchOptions()
fetchOptions.fetchLimit = 1000
let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: false)
fetchOptions.sortDescriptors = [sortDescriptor]
let allPhotos = PHAsset.fetchAssets(with: .video, options: fetchOptions)
allPhotos.enumerateObjects {
[weak self] (asset, count, stop) in
if asset.playbackStyle == .livePhoto {
return
}
let imageManager = PHImageManager.default()
let targetSize = CGSize(width: 350, height: 350)
let options = PHImageRequestOptions()
options.isSynchronous = true
imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: {
[weak self] (image, info) in
if let image = image {
self?.tableData.append(image)
}
if count == allPhotos.count - 1 {
self?.collectionView.reloadData()
}
})
}
}
您可以使用 PHFetchOptions
的 predicate
属性。如果资产的 mediaSubtype
属性指示它是实时照片,则将谓词设置为失败。
fetchOptions.predicate = NSPredicate(format: "(mediaSubtype & %ld) == 0", PHAssetMediaSubtype.PhotoLive.rawValue)
在使用 UIImagePicker
时遵循 @matt 的 code,我可以防止用户在选择图像后选择 .livePhoto
使用:
let asset = info[UIImagePickerControllerPHAsset] as? PHAsset
if asset?.playbackStyle == .livePhoto {
// alert user this photo isn't a possibility
}
使用 PHFetchOptions 时,如何防止它们显示而不是在 enumerateObjects 回调中过滤掉它们?
fileprivate func fetchPhotos() {
let fetchOptions = PHFetchOptions()
fetchOptions.fetchLimit = 1000
let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: false)
fetchOptions.sortDescriptors = [sortDescriptor]
let allPhotos = PHAsset.fetchAssets(with: .video, options: fetchOptions)
allPhotos.enumerateObjects {
[weak self] (asset, count, stop) in
if asset.playbackStyle == .livePhoto {
return
}
let imageManager = PHImageManager.default()
let targetSize = CGSize(width: 350, height: 350)
let options = PHImageRequestOptions()
options.isSynchronous = true
imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: {
[weak self] (image, info) in
if let image = image {
self?.tableData.append(image)
}
if count == allPhotos.count - 1 {
self?.collectionView.reloadData()
}
})
}
}
您可以使用 PHFetchOptions
的 predicate
属性。如果资产的 mediaSubtype
属性指示它是实时照片,则将谓词设置为失败。
fetchOptions.predicate = NSPredicate(format: "(mediaSubtype & %ld) == 0", PHAssetMediaSubtype.PhotoLive.rawValue)