2016-09-11 25 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

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

相關問題