2016-09-30 54 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爲你的帽子戲法幫助,也完成了我的任務歡呼:-) –

相關問題