PHFetchResults 日期过滤器没有为时间范围生成正确的结果 [英] PHFetchResults date filter not generating correct results for time range

查看:150
本文介绍了PHFetchResults 日期过滤器没有为时间范围生成正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从两个日期和 我成功获取图像. 使用 PHAsset 库

I am trying to fetch images from photo library within the range of two dates and I am getting the images successfully. Using PHAsset Library

现在的问题是我无法在同一天的两次之间获取图像,例如在 12:10PM 到 12:30PM 之间

Now the problem is that am not able to get images between two times of the same day like between 12:10PM to 12:30PM

使用下面的代码

    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;
} 

-- 并将这些日期传递给以下方法以获取图像

-- And passing those dates to below method to get images

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

当我在日志中打印但没有获取图像时,日期和时间正确

Dates and time coming correct when i print in log but not getting images

如何解决这个问题?

推荐答案

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

The date created here would be in UTC timezone and yours may vary. Which is why the predicate may not return you correct results.

在从中创建日期之前,请确保已将日历的时区设置为系统时区,例如:

Make sure you have set the timezone of calendar to your system timezone before creating a date from it like:

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

这将在您当地的时区创建日期并生成正确的结果.

That will create the date in your local timezone and generate correct results.

这篇关于PHFetchResults 日期过滤器没有为时间范围生成正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆