使用以下代碼時,它會獲取我的最後一張照片,但它會複製它。它總是得到最後一張照片,但它返回一對:從用戶照片庫獲取最後一張照片返回兩張照片
UIImageView *imageView = (UIImageView *) [self.photoCollectionView viewWithTag:101];
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
PHAsset *lastAsset = [fetchResult lastObject];
[[PHImageManager defaultManager] requestImageForAsset:lastAsset
targetSize:imageView.bounds.size
contentMode:PHImageContentModeAspectFill
options:nil
resultHandler:^(UIImage *result, NSDictionary *info) {
NSLog(@"PHImageManager request results %@ and info %@", result, info);
dispatch_async(dispatch_get_main_queue(), ^{
[user_photos addObject:result];
NSLog(@"%lu",user_photos.count);
[self.photoCollectionView reloadData];
NSLog(@"%lu",user_photos.count);
});
}];
爲什麼會出現這種情況?下面是日誌語句:
2015-09-06 21:48:22.153[41357:11390462] PHImageManager request results <UIImage: 0x7fd1336b8f40>, {60, 40} and info {
PHImageFileOrientationKey = 0;
PHImageResultDeliveredImageFormatKey = 4031;
PHImageResultIsDegradedKey = 1;
PHImageResultRequestIDKey = 1;
PHImageResultWantedImageFormatKey = 5003;
}
2015-09-06 21:48:22.158[41357:11390462] 2
2015-09-06 21:48:22.159[41357:11390462] 2
2015-09-06 21:48:22.166[41357:11390462] PHImageManager request results <UIImage: 0x7fd13374c3b0>, {386, 256} and info {
PHImageFileOrientationKey = 0;
PHImageResultDeliveredImageFormatKey = 5003;
PHImageResultIsDegradedKey = 0;
PHImageResultRequestIDKey = 1;
PHImageResultWantedImageFormatKey = 5003;
}
2015-09-06 21:48:22.167[41357:11390462] 3
2015-09-06 21:48:22.167[41357:11390462] 3
這發生在模擬器上&設備。不使用第三方軟件,我認爲這不重要。爲什麼會被叫兩次?我打電話給它在一個uialertaction塊