iosswiftphotosframework

How to get only images in the camera roll using Photos Framework


The following code loads images that are also located on iCloud or the streams images. How can we limit the search to only images in the camera roll?

var assets = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: nil)

Solution

  • Through some experimentation we discovered a hidden property not listed in the documentation (assetSource). Basically you have to do a regular fetch request, then use a predicate to filter the ones from the camera roll. This value should be 3.

    Sample code:

    //fetch all assets, then sub fetch only the range we need
    var assets = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions)
    
    assets.enumerateObjectsUsingBlock { (obj, idx, bool) -> Void in
        results.addObject(obj)
    }
    
    var cameraRollAssets = results.filteredArrayUsingPredicate(NSPredicate(format: "assetSource == %@", argumentArray: [3]))
    results = NSMutableArray(array: cameraRollAssets)