簡體   English   中英

從IOS的相冊中獲取今天的照片

[英]Get today's photo from album in IOS

有沒有辦法從ios中的專輯中獲取今天的照片?我知道如何獲取專輯,但所有照片都顯示為時間軸。 我只想獲得今天的照片或最近兩天的照片,我怎么能意識到這一點? 謝謝。

Swift 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)

您可以使用此片段獲取今天的照片,該照片適用於iOS 8.我最初從最近添加的相冊中過濾了資產,該相冊存儲了過去30天的照片或1000張照片。 用戶有可能在兩天內拍攝超過1000張照片,因此我更改了代碼以從庫中獲取所有照片。

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

在iOS 8之前,使用ALAssetsLibrary,假設您有一個照片組,以相反的順序枚舉該組,並執行與上面類似的操作。

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

你可以在當天使用謂詞

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM