PHFetchResults 日期过滤器未生成时间范围的正确结果

2024-01-03

我正在尝试从照片库中获取两个日期范围内的图像我成功获取图像。 https://stackoverflow.com/questions/39587405/how-to-fetch-images-from-photo-library-within-range-of-two-dates-in-ios使用 PHAsset 库

现在的问题是我无法在同一天的两个时间之间获取图像,例如 12:10PM 到 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;
} 

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

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

当我打印日志但未获取图像时,日期和时间正确

如何解决这个问题呢 ?


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

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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PHFetchResults 日期过滤器未生成时间范围的正确结果 的相关文章

随机推荐