我試圖從保存的相冊中創建一個匹配特定條件的所有圖像的數組。這是一個簡化的代碼。我將這些照片添加到myImages數組中,並通過「已添加圖像」日誌進行確認,以便記錄正確的圖像。但是函數返回的數組總是空的。相當新的Objective-C,所以任何建議都會有幫助。數組保存的照片總是返回爲空
NSMutableArray * myImages = [NSMutableArray array];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// Within the group enumeration block, filter to enumerate just photos.
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
// The end of the enumeration is signaled by asset == nil.
if (alAsset) {
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullResolutionImage]];
NSLog(@"Added Image");
[myImages addObject:latestPhoto];
}
}];
}
failureBlock: ^(NSError *error) {
// Typically you should handle an error more gracefully than this.
NSLog(@"No groups");
}];
return myImages;
是的,imagesTakenOnDate應該是myImages。我會閱讀你的建議並嘗試。謝謝! – vishwa