2014-10-12 5 views
3

오늘의 사진을 ios에서 가져 오는 방법이 있습니까? 앨범을 얻는 방법을 알고 있지만 모든 사진을 타임 라인으로 표시합니다. 나는 오늘의 사진이나 지난 2 일간의 사진을 얻고 싶습니다. 어떻게 실현할 수 있습니까? 감사합니다. .IOS에서 앨범의 오늘 사진 가져 오기

+0

가 AssetLibrary을 사용하여 수행 프레임 워크 : http://stackoverflow.com/questions/18575691/filter-alassets-by-year –

답변

5

이 스 니펫을 사용하여 iOS 8에서 작동하는 오늘의 사진을 얻을 수 있습니다. 처음에는 최근 30 일 또는 1000 장의 사진을 저장하는 최근 추가 된 앨범에서 애셋을 필터링했습니다. 2 일 만에 1000 장 이상의 사진을 캡처 할 수있는 기회가 생겨서 라이브러리에서 모든 사진을 가져 오도록 코드를 변경했습니다. ALAssetsLibrary를 사용

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

이전 아이폰 OS 8, 당신은 사진 그룹이 있다고 가정 역순으로 그룹을 열거하고, 위와 비슷한 일을한다.

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

여기에 일부 코드를 입력하지 마십시오. 적어도 그것이하는 일을하는 이유를 설명하는 짧은 문장을 추가하십시오. - 이것은 또한 ios8 –

+0

@ Daij-Djan에 대해서만 언급합니다. 맞습니다. 알려 주셔서 고마워요. – gabbler

+0

cool - revesered 내 투표 –

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

이것은 실제로 허용 된 것보다 훨씬 나은 대답입니다. 모든 이미지를 가져온 다음 필터링하지 않아도됩니다. 단 한 줄의 코드만으로도 충분합니다. :) –

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

두 번째 인수 유형에 오타가 있습니다. –

+0

감사합니다. 그것은 오래 지속되었습니다. :) –

+0

방금 ​​잘못 바꿨습니다. "CVarArg"는 정확했지만 두 번째 매개 변수에는 "CVarGArg"가 있습니다. 오해해서 미안해 ;) –

관련 문제