2
votes

Hi I have a question about PHImageFetch

I'm trying to fetch the recent created images using the PHFetchOptions.

But It always return the past created images.

Here is my simple code. Anyone please helping me?

It always return past created photos from library.

override func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {


            let options = PHImageRequestOptions()
            options.networkAccessAllowed = true
            options.synchronous = false

//I want use this!
            let fetchOptions = PHFetchOptions()
            fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
            //

            PHImageManager.defaultManager().requestImageForAsset(assets?[indexPath.row] as! PHAsset, targetSize: CGSizeMake(114, 114), contentMode: PHImageContentMode.AspectFill, options: options) { (image, info) -> Void in
                if (image != nil) {
                    (cell as! PhotoCell).image = image
                }
            }



    }

And I'm trying to fetch recent created photos via PHFetchOptions.

I already trying to set "fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]" or "fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]"

But It is same result...past created photos.

And Is this logic fine when I fetching the recent created images then set the image to the cell in the override func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) ?.

override func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {

        if isGellery == true {



            let options = PHImageRequestOptions()
            options.networkAccessAllowed = true
            options.synchronous = false

            let fetchOptions = PHFetchOptions()
            fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]

            // Sort the images by creation date

            //
           if let fetchResult: PHFetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions) {
//                
//                // If the fetch result isn't empty,
//                // proceed with the image request
               if fetchResult.count > 0 {
//                    // Perform the image request
                    imgManager.requestImageForAsset(assets?[indexPath.row] as! PHAsset, targetSize: CGSizeMake(114, 114), contentMode: PHImageContentMode.AspectFill, options: requestOptions, resultHandler: { (image, _) in

                        if (image != nil) {
                            (cell as! PhotoCell).image = image
                        }
                    })
                }
            }


        }


    }

block is what I had trying to fetch.

I think it is problem about indexPath.row..

1
You say it isn't working, but what is actually happening? Do you ever actually populate the assets array elsewhere in this class?Thomas Abend
Thanks for the reply. I updated my question. It is working but I want retrieve order by recent created images not past created images. Could you helping me?Shawn Baek

1 Answers

3
votes

Maybe you should grab all your photos in a dedicated method. Here, each time willDisplayCell is called, you loop through all your photos.

Here is my method to grab all user's photos by their creation date.

func grabPhotos(){

    let imgManager = PHImageManager.defaultManager()

    let requestOptions = PHImageRequestOptions()
    requestOptions.synchronous = true
    requestOptions.deliveryMode = .HighQualityFormat

    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

    if let fetchResult : PHFetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions){

        if fetchResult.count > 0{

            for i in 0..<fetchResult.count{
               let asset =  fetchResult.objectAtIndex(i) as! PHAsset

                imgManager.requestImageForAsset(fetchResult.objectAtIndex(i) as! PHAsset, targetSize: CGSize(width: 200,height: 200), contentMode: .AspectFill, options: requestOptions, resultHandler: { (image, error) in
                    self.imageArray.append(image!)
                })
            }
        }
        else{
            print("you got no photos")
            self.collectionView?.reloadData()
        }
    }
}

Take a look at this good tutorial : https://www.youtube.com/watch?v=BFZ4ZCw_9z4