【发布时间】:2017-01-19 08:24:12
【问题描述】:
您好,我有一个关于 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(key: "creationDate", 升序: false)]"
但结果是一样的...过去创建的照片。
当我获取最近创建的图像然后将图像设置为 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 是我试图获取的内容。
我认为是 indexPath.row 的问题..
【问题讨论】:
-
你说它不起作用,但实际上发生了什么?你有没有在这个类的其他地方实际填充过 assets 数组?
-
感谢您的回复。我更新了我的问题。它正在工作,但我想通过最近创建的图像而不是过去创建的图像来检索订单。你能帮帮我吗?
标签: swift uicollectionviewcell