iosiphoneswiftphassetphassetcollection

Get Specific Photo from Specific Photo Album with PHAssetCollection in Swift 4


I am storing the photo with the use of following code. But now I have to get the photos from storage using name.

First Question: How can I store the photos using name ? Should I store it in Core Data ?

Second Question: How can I get that photo using the name I have given it previously ?

Please Help me. Thank you for all replies ...

class CustomPhotoAlbum: NSObject {
static let albumName = "PhotoAlbumName"
static let sharedInstance = CustomPhotoAlbum()

var assetCollection: PHAssetCollection!

override init() {
    super.init()

    if let assetCollection = fetchAssetCollectionForAlbum() {
        self.assetCollection = assetCollection
        return
    }

    if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.authorized {
        PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) -> Void in
            ()
        })
    }

    if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized {
        self.createAlbum()
    } else {
        PHPhotoLibrary.requestAuthorization(requestAuthorizationHandler)
    }
}

func requestAuthorizationHandler(status: PHAuthorizationStatus) {
    if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized {
        // ideally this ensures the creation of the photo album even if authorization wasn't prompted till after init was done
        print("trying again to create the album")
        self.createAlbum()
    } else {
        print("should really prompt the user to let them know it's failed")
    }
}

func createAlbum() {
    PHPhotoLibrary.shared().performChanges({
        PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: CustomPhotoAlbum.albumName)   // create an asset collection with the album name
    }) { success, error in
        if success {
            self.assetCollection = self.fetchAssetCollectionForAlbum()
        } else {
            print("error \(String(describing: error))")
        }
    }
}

func fetchAssetCollectionForAlbum() -> PHAssetCollection? {
    let fetchOptions = PHFetchOptions()
    fetchOptions.predicate = NSPredicate(format: "title = %@", CustomPhotoAlbum.albumName)
    let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)

    if let _: AnyObject = collection.firstObject {
        return collection.firstObject
    }
    return nil
}

func save(image: UIImage) {
    if assetCollection == nil {
        return                          // if there was an error upstream, skip the save
    }

    PHPhotoLibrary.shared().performChanges({
        let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
        let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
        let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection)
        let enumeration: NSArray = [assetPlaceHolder!]
        albumChangeRequest!.addAssets(enumeration)

    }, completionHandler: nil)
}

}


Solution

  • You can use this class .

    import Foundation
    import Photos
    
    class FetchPhotos{
    
        var images:[UIImage] = []
        var imageUrl:String!
    
        func fetchPhotos() -> String{
            let fetchOptions = PHFetchOptions()
            fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: false)]
    
            // Fetch the image assets
            let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)
    
            // If the fetch result isn't empty,
            // proceed with the image request
            if fetchResult.count > 0 {
                let totalImageCountNeeded = 1 // <-- The number of images to fetch
                fetchPhotoAtIndex(0, totalImageCountNeeded, fetchResult)
            }
            return imageUrl
        }
    
    
        // Repeatedly call the following method while incrementing
        // the index until all the photos are fetched
        func fetchPhotoAtIndex(_ index:Int, _ totalImageCountNeeded: Int, _ fetchResult: PHFetchResult<PHAsset>) {
    
            let albumName = "CustonAlbum"
            // Note that if the request is not set to synchronous
            // the requestImageForAsset will return both the image
            // and thumbnail; by setting synchronous to true it
            // will return just the thumbnail
            let requestOptions = PHImageRequestOptions()
            requestOptions.isSynchronous = true
    
            let collection: PHFetchResult = 
    PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)
            for k in 0 ..< collection.count {
                let obj:AnyObject! = collection.object(at: k)
                if obj.title == albumName {
                    print("Yeap!")
    
                    // Perform the image request
                    PHImageManager.default().requestImage(for: fetchResult.object(at: index) as PHAsset, targetSize: CGSize(width: 1000, height: 1000), contentMode: PHImageContentMode.aspectFill, options: requestOptions, resultHandler: { (image, info) in
    
                        if let image = image {
                            // Add the returned image to your array
                            self.images += [image]
                        }
                        // If you haven't already reached the first
                        // index of the fetch result and if you haven't
                        // already stored all of the images you need,
                        // perform the fetch request again with an
                        // incremented index
                        if index + 1 < fetchResult.count && self.images.count < totalImageCountNeeded {
                            self.fetchPhotoAtIndex(index + 1, totalImageCountNeeded, fetchResult)
                        } else {
                            // Else you have completed creating your array
                            print("Completed array: \(self.images[0])")
                            print("Image URL \(String(describing: info!["PHImageFileURLKey"]))")
                            self.imageUrl = "\(info!["PHImageFileURLKey"]!)"
                        }
                    })
    
                }}
        }
    
    
    }