2016-09-11 76 views
2

您好我有一个关于PHImageFetch的问题如何在单元中获取最近创建的照片?

我试图使用PHFetchOptions获取最近创建的图像。

但它总是返回过去创建的图像。

这是我的简单代码。任何人都请帮助我?

它总是返回从库中创建的照片。

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 
       } 
      } 



    } 

而我试图通过PHFetchOptions获取最近创建的照片。

我已经尝试设置 “fetchOptions.sortDescriptors = [NSSortDescriptor(关键: ”creationDate“,上升:真)” 或 “fetchOptions.sortDescriptors = [NSSortDescriptor(关键: “creationDate”,上升:假)]“

但它是相同的结果...过去创建的照片。

,是当我取最近创建的图像,然后在覆盖FUNC的CollectionView图像设置为单元格这样的逻辑罚款(的CollectionView:UICollectionView,willDisplayCell细胞: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 
         } 
        }) 
       } 
      } 


     } 


    } 

块是我试图获取。

,我认为它是关于indexPath.row问题..

+1

你说这是行不通的,但什么是实际发生的?你有没有在这个类的其他地方填充assets数组? –

+0

感谢您的回复。我更新了我的问题。它正在工作,但我想通过最近创建的图像检索顺序,而不是过去创建的图像。你能帮我吗? –

回答

3

也许你应该抓住所有的照片在一个专用的方法。 在这里,每次调用willDisplayCell时,都会遍历所有照片。

这里是我的方法来抓住他们的创建日期的所有用户的照片。

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() 
     } 
    } 
} 

看看这个好教程:https://www.youtube.com/watch?v=BFZ4ZCw_9z4

+0

哇这真的太棒了!我非常感谢你。你拯救了我的一天。再次感谢 :) –

相关问题