您好我有一個關於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問題..
你說這是行不通的,但什麼是實際發生的?你有沒有在這個類的其他地方填充assets數組? –
感謝您的回覆。我更新了我的問題。它正在工作,但我想通過最近創建的圖像檢索順序,而不是過去創建的圖像。你能幫我嗎? –