오늘의 사진을 ios에서 가져 오는 방법이 있습니까? 앨범을 얻는 방법을 알고 있지만 모든 사진을 타임 라인으로 표시합니다. 나는 오늘의 사진이나 지난 2 일간의 사진을 얻고 싶습니다. 어떻게 실현할 수 있습니까? 감사합니다. .IOS에서 앨범의 오늘 사진 가져 오기
답변
이 스 니펫을 사용하여 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];
}];
여기에 일부 코드를 입력하지 마십시오. 적어도 그것이하는 일을하는 이유를 설명하는 짧은 문장을 추가하십시오. - 이것은 또한 ios8 –
@ Daij-Djan에 대해서만 언급합니다. 맞습니다. 알려 주셔서 고마워요. – gabbler
cool - revesered 내 투표 –
당신은 날
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];
}
이것은 실제로 허용 된 것보다 훨씬 나은 대답입니다. 모든 이미지를 가져온 다음 필터링하지 않아도됩니다. 단 한 줄의 코드만으로도 충분합니다. :) –
스위프트 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)
두 번째 인수 유형에 오타가 있습니다. –
감사합니다. 그것은 오래 지속되었습니다. :) –
방금 잘못 바꿨습니다. "CVarArg"는 정확했지만 두 번째 매개 변수에는 "CVarGArg"가 있습니다. 오해해서 미안해 ;) –
- 1. 특정 앨범의 사진 가져 오기 Facebook SDK
- 2. 모든 앨범의 모든 사진 가져 오기
- 3. flickr api를 사용하여 지정된 앨범의 사진 및 앨범 가져 오기
- 4. Facebook API PHP 사용자의 "프로필 사진"앨범의 ID 가져 오기
- 5. 각 앨범의 모든 사진 앨범 및 축소판 목록 가져 오기
- 6. Facebook - 페이지 사진 가져 오기
- 7. 앨범 표지 사진 가져 오기
- 8. 아이폰 장치에서 사진 앨범의 경로
- 9. "사진"에있는 앨범의 동영상 재생
- 10. 사진 라이브러리에서 사진 가져 오기
- 11. MusicBrainz ID로 앨범의 트랙 목록 가져 오기
- 12. 트위터에서 사진 가져 오기
- 13. 스트림에서 사진 가져 오기
- 14. 카메라에서 사진 가져 오기
- 15. PDF에서 사진 가져 오기
- 16. Instagram에서 사진 가져 오기
- 17. 사진 가져 오기 옵션
- 18. 아이폰에서 사진 가져 오기 사진 라이브러리
- 19. 앨범의 첫 번째 엄지 사진 선택
- 20. Facebook 커버 사진 앨범의 사진 ID를 얻으려면 어떻게해야합니까?
- 21. 페이지의 프로필 사진 가져 오기
- 22. 사용자의 Facebook 사진 가져 오기
- 23. 좌표로 Google지도에서 사진 가져 오기
- 24. 동기식으로 사진 가져 오기 (우리에서)
- 25. facebook 표지 사진 가져 오기
- 26. facebook 프로필 사진 가져 오기
- 27. 메인 계정 사진 가져 오기
- 28. ALAssetsLibrary 위치에서 사진 가져 오기
- 29. android에서 연락처 사진 가져 오기
- 30. 업데이트 성명 - 사진 가져 오기
가 AssetLibrary을 사용하여 수행 프레임 워크 : http://stackoverflow.com/questions/18575691/filter-alassets-by-year –