3

I am using a chunk from Apple's sample code here:

override func awakeFromNib() {
    // Create a PHFetchResult object for each section in the table view.
    let allPhotosOptions = PHFetchOptions()
    allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

    let allPhotos = PHAsset.fetchAssetsWithOptions(allPhotosOptions)
    let smartAlbums = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .AlbumRegular, options: nil)

    let topLevelUserCollections = PHCollectionList.fetchTopLevelUserCollectionsWithOptions(nil)

    // Store the PHFetchResult objects and localized titles for each section.
    self.sectionFetchResults = [allPhotos, smartAlbums, topLevelUserCollections]
    self.sectionLocalizedTitles = ["", NSLocalizedString("Smart Albums", comment: ""), NSLocalizedString("Albums", comment: "")]

    PHPhotoLibrary.sharedPhotoLibrary().registerChangeObserver(self)
}

This lists all Albums successfully.

What I Need:

I want to only list albums with photos, exclude videos. Also, exclude video's from getting listed inside of albums, such as inside "All Photos".

What I tried:

let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.Image.rawValue)

This causes to crash saying 'Unsupported predicate in fetch options: mediaType == 1'

Gizmodo
  • 3,151
  • 7
  • 45
  • 92
  • 1
    This question mentions that asset collections don't have `mediaType`s (makes sense, they can contain multiple types): http://stackoverflow.com/q/35590640 You don't show how you apply the predicate, but I guess you'll have to filter the the collections some other way. – Rhythmic Fistman Sep 05 '16 at 03:13
  • @RhythmicFistman So far I believe cellForItemAtIndexPath is where I have to check and filter. – Gizmodo Sep 05 '16 at 18:04
  • 1
    isn't that too late? haven't you already told the collection view about the unwanted non-images in `numberOfItemsInSection` at that point? I think you should filter before then. – Rhythmic Fistman Sep 06 '16 at 00:05
  • 1
    That is true! So that wouldn't work.. – Gizmodo Sep 06 '16 at 00:08

2 Answers2

4

I know this is a very late answer, but in case someone like me gets here, you should use this method to retrieve a media type. In this case PHAssetMediaType.image

func fetchAssets(with mediaType: PHAssetMediaType, options: PHFetchOptions?) -> PHFetchResult<PHAsset>

So in Swift 5 it writes as

let allPhotosOptions = PHFetchOptions()
allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchResult = PHAsset.fetchAssets(with: .image, options: allPhotosOptions)
dequin
  • 906
  • 12
  • 19
1

So far it works fine to me :

let allAlbums = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .albumRegular, options: nil)
let someAlbum = allAlbums.object(at: 0)
let onlyPhotoOption = PHFetchOptions()
onlyPhotoOption.predicate = NSPredicate(format: "mediaType == %i", PHAssetMediaType.image.rawValue)
let photos = PHAsset.fetchAssets(in: someAlbum, options: onlyPhotoOption)
Eric Yuan
  • 1,213
  • 11
  • 13