0

Following @matt's code when using the UIImagePicker, I can prevent the user from picking a .livePhoto once an image is choosen using:

let asset = info[UIImagePickerControllerPHAsset] as? PHAsset

if asset?.playbackStyle == .livePhoto { 
   // alert user this photo isn't a possibility 
}

When using the PHFetchOptions how can I prevent them from being shown instead of filtering them out inside the enumerateObjects callback?

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()
            }
        })
    }
}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256

1 Answers1

1

You can use the predicate property of PHFetchOptions. Setup the predicate to fail if the mediaSubtype attribute of the asset indicates it is a live photo.

fetchOptions.predicate = NSPredicate(format: "(mediaSubtype & %ld) == 0", PHAssetMediaSubtype.PhotoLive.rawValue)
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • lol, thanks, @matt had actually answered then question for me in a separate thread and I posted the question if anyone else came across the problem. I was expecting him to answer it. Yes your answer is correct. Thanks. I'll upvote it for now and if he doesn't answer I'll accept your as the official answer. Awkward situation. – Lance Samaria Sep 12 '18 at 01:58
  • Are you saying there is another SO question that already has this answer? Where? And never post a question expecting a specific person to answer. And if you already knew the answer then you should have posted your own answer. – rmaddy Sep 12 '18 at 02:00
  • No it was a thread like the one we're in now. The question was part of a conversation. He sent the answer inside the convo and I was actually about to post what he suggested temporarily until he answered. Your correct though once a question is up anyone can answer it. Thanks for the advice. Look at the last two comments in the answer thread https://stackoverflow.com/questions/49413685/why-is-imagepickercontroller-mediatypes-kuttypemovie-as-string-including-liv/49414324?noredirect=1#comment91520593_49414324 – Lance Samaria Sep 12 '18 at 02:04