Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Select All Photos From Specific Photos Album

Tags:

ios

swift

The app can create a custom album in the standard iOS photos application, but I have been unable to find a way for the app to gather all the images from that album to be displayed within the app.

Currently, the app is able to gather images from all the albums, just not one is particular.

let resultCollections = PHAssetCollection.fetchAssetCollectionsWithType(
                 .Album,
        subtype: .AlbumRegular,
        options: nil)

    resultCollections.enumerateObjectsUsingBlock({
        (object, index, stop) -> Void in

        let collection = object as! PHAssetCollection
        let result = PHAsset.fetchAssetsInAssetCollection(collection, options: nil)

        result.enumerateObjectsUsingBlock({
            (object, index, stop) -> Void in

            let asset = object as! PHAsset
            self.images.append(asset)

        })

    })

I have seen other questions that might be marked as duplicates, however the majority of them are talking about opening a UIPickerView to a custom album. This is a possible duplicate of How to fetch all images from custom Photo Album - swift however, it was never answered.

So, how can an iOS app gather all images from a particular photos album?

like image 467
rcobelli Avatar asked Sep 24 '15 02:09

rcobelli


People also ask

How do I select all photos?

How to select multiple photos on Android. Selecting all the photos saved locally on an Android phone is a lot easier than doing so on an iPhone: On stock Android, open the Files app and go to Images, tap the three dots in the top right corner of your screen, and hit Select all.

How do I move photos to a specific album?

Touch and hold a photo, and then select the photos you want in your new album. Select Album. Optional: Add a title to your new album.

Can you organize iPhone photos?

Use albums in the Photos app to view and organize your photos and videos. Tap Albums to view your photos and videos organized into different categories and media types, like Videos, Portrait, and Slo-mo.


1 Answers

Add fetchOptions like below

let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", YourAlbumTitle)
let resultCollections = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .AlbumRegular, options: fetchOptions)

Actually, album title isn't unique value, they can duplicate. so I recommend use localIdentifier like below, if your app access multiple albums.

let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "localIdentifier = %@", YourAlbumLocalIdentifier)
let resultCollections = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .AlbumRegular, options: fetchOptions)
like image 61
Inhan Avatar answered Oct 04 '22 20:10

Inhan