如何在单元格中获取最近创建的照片?
How do I fetch recent created photos in cell?
你好,我有一个关于 PHImageFetch 的问题
我正在尝试使用 PHFetchOptions 获取最近创建的图像。
但它总是return过去创造的图像。
这是我的简单代码。有人可以帮助我吗?
它总是 return 过去从库中创建的照片。
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(key: "creationDate", 升序: true)]"
要么
"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
}
})
}
}
}
}
块是我试图获取的内容。
我认为是关于indexPath.row的问题..
也许你应该用专门的方法抓取你所有的照片。
在这里,每次调用 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()
}
}
}
你好,我有一个关于 PHImageFetch 的问题
我正在尝试使用 PHFetchOptions 获取最近创建的图像。
但它总是return过去创造的图像。
这是我的简单代码。有人可以帮助我吗?
它总是 return 过去从库中创建的照片。
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(key: "creationDate", 升序: true)]" 要么 "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
}
})
}
}
}
}
块是我试图获取的内容。
我认为是关于indexPath.row的问题..
也许你应该用专门的方法抓取你所有的照片。 在这里,每次调用 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()
}
}
}