2016-09-30 50 views
2

我想两个日期和I am getting the images successfully.的范围内获取从照片库影像使用PHAsset图书馆PHFetchResults日期过滤器不产生时间范围内正确的结果

现在的问题是,我不能够两者之间获得的图像像12:10 PM之间在同一天的时间到12:30 PM

使用下面的代码

NSDate *startDate = [self getDateForDay:30 andMonth:9 andYear:2016 andHour:12 andMinute:10 andSecond:0]; 
    NSDate *endDate = [self getDateForDay:30 andMonth:9 andYear:2016 andHour:12 andMinute:30 andSecond:0]; 

-

-(NSDate*) getDateForDay:(NSInteger) day andMonth:(NSInteger) month andYear:(NSInteger) year andHour:(NSInteger) hour andMinute:(NSInteger) minute andSecond:(NSInteger) second{ 
NSDateComponents *comps = [[NSDateComponents alloc] init]; 
[comps setDay:day]; 
[comps setMonth:month]; 
[comps setYear:year]; 

[comps setHour:hour]; 
[comps setMinute:minute]; 
[comps setSecond:second]; 
NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; 

NSDate *date = [gregorian dateFromComponents:comps]; 
return date; 
} 

- 然后把这些日期如下方法来获得图像

PHFetchResult *result = [self getAssetsFromLibraryWithStartDate:startDate andEndDate:endDate]; 

日期和时间来正确的,当我在日志打印,但没有得到图像

如何解决这个问题呢?

+1

您是否正确设置了时区?此处创建的日期将采用UTC时区,而您的日期可能会有所不同。确保你已经设置了日历的时区,例如'[calendar setTimeZone:[NSTimeZone systemTimeZone]];'在创建日期之前 – NSNoob

+1

并且还要确保在时间范围内存在库中的图像 – NSNoob

+0

检查我编辑的问题...雅我有图像肯定 –

回答

1

此处创建的日期将采用UTC时区,您的可能会有所不同。这就是为什么谓词可能不会返回正确结果的原因。

确保您已创建从它的日期像以前一样日历的时区设置为你的系统时区:

NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
[gregorian setTimeZone: [NSTimeZone systemTimeZone]]; 
NSDate *date = [gregorian dateFromComponents:comps]; 

这将创建一个在你的本地时区的日期,并产生正确的结果。

+2

雅得到它了...非常感谢你@ NSNoob为你的帽子戏法帮助,也完成了我的任务欢呼:-) –

相关问题