2014-10-12 136 views
3

有没有什么办法从ios中的相册中获取当天的照片? 我知道如何获取相册,但所有照片都显示为时间线。 我只想得到今天的照片或最近两天的照片,我怎么能意识到这一点? 谢谢。从IOS中的相册获取今天的照片

+0

使用素材资源库做吧框架: http://stackoverflow.com/questions/18575691/filter-alassets-by-year – 2014-10-12 08:34:17

回答

5

您可以使用此片段获取适用于iOS 8的今日照片。我最初过滤了最近添加的相册中的资产,该相册可存储过去30天或1000张照片中的照片。用户有机会在两天内拍摄超过1000张照片,因此我更改了代码以获取库中的所有照片。

PHFetchOptions *options = [[PHFetchOptions alloc] init]; 
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]; 
options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeImage]; 

PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsWithOptions:options]; 

//get day component of today 
NSCalendar* calendar = [NSCalendar currentCalendar]; 
NSDateComponents *dayComponent = [calendar components:NSCalendarUnitDay fromDate:[NSDate date]]; 
NSInteger currentDay = dayComponent.day; 

//get day component of yesterday 
dayComponent.day = - 1; 
NSDate *yesterdayDate = [calendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0]; 
NSInteger yesterDay = [[calendar components:NSCalendarUnitDay fromDate:yesterdayDate] day]; 

//filter assets of today and yesterday add them to an array. 
NSMutableArray *assetsArray = [NSMutableArray array]; 
for (PHAsset *asset in assetsFetchResult) { 
    NSInteger assetDay = [[calendar components:NSCalendarUnitDay fromDate:asset.creationDate] day]; 

    if (assetDay == currentDay || assetDay == yesterDay) { 
     [assetsArray addObject:asset]; 
    } 
    else { 
     //assets is in descending order, so we can break here. 
     break; 
    } 
} 

此前的iOS 8,使用ALAssetsLibrary,假设你有一个照片组,枚举组以相反的顺序,并做类似的事情,如上。

[self.photoGroup enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) { 
     NSDate *date = [asset valueForProperty:ALAssetPropertyDate]; 
    }]; 
+0

不只是在这里放一些代码。至少要添加一个简短的句子来解释它为什么会这样做。 - 还提到这只适用于ios8 – 2014-10-12 08:29:47

+0

@ Daij-Djan,你说得对,谢谢你让我知道。 – gabbler 2014-10-12 09:46:49

+0

酷 - 重新启动了我的投票 – 2014-10-12 09:49:42

2

你可以使用一个谓词的一天

PHFetchOptions *allPhotosOptions = [PHFetchOptions new]; 
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; 

NSPredicate *predicateMediaType = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeImage]; 
NSDate *date = [[NSDate date] beginningOfDay]; 
NSPredicate *predicateDate = [NSPredicate predicateWithFormat:@"creationDate >= %@", date]; 

NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicateDate, predicateMediaType]]; 


allPhotosOptions.predicate = compoundPredicate; 

PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions]; 

@implementation NSDate (Utils) 

- (NSDate *)beginningOfDay { 
NSCalendar *calendar = [NSCalendar currentCalendar]; 

    NSDateComponents *components = [calendar components:CYCalendarUnitYear | CYCalendarUnitMonth | CYCalendarUnitDay fromDate:self]; 

    return [calendar dateFromComponents:components]; 
} 
+0

这实际上比接受的答案要好得多。它不需要获取所有图像,然后将其过滤掉。只需一行代码。 :) – 2016-11-14 10:35:03

7

斯威夫特3版本日期范围:

let fromDate = // the date after which you want to retrieve the photos 
let toDate // the date until which you want to retrieve the photos 

let options = PHFetchOptions() 
options.predicate = NSPredicate(format: "creationDate > %@ && creationDate < %@", fromDate as CVarArg, toDate as CVarArg) 

//Just a way to set order 
let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: false) 
options.sortDescriptors = [sortDescriptor] 

return PHAsset.fetchAssets(with: .image, options: options) 
+0

您在第二个参数类型中有错字。 – 2017-04-18 06:29:03

+0

谢谢。这持续了很长时间。 :) – 2017-04-18 06:30:35

+0

你只是改变它错了。 “CVarArg”是正确的,只是在第二个参数中有“CVarGArg”。我为我的误会道歉 ;) – 2017-04-18 06:34:02

相关问题