Getting last picture taken from users photo library returning two pictures

987 Views Asked by At

When using the following code, it gets my last picture, but it duplicates it. It always gets the last picture, but it returns in a pair:

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

Any reason why this is happening? Here are the log statements:

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

This happens in simulator & on device. not using 3rd party software, not that would matter, i think. why is it getting called twice? i'm calling it in a uialertaction block

2

There are 2 best solutions below

0
On

Replace PHAsset *lastAsset=[fetchResult lastObject]; in your code with just

PHAsset *lastAsset=fetchResult;
0
On

Well after doing some research on what "PHImageResultIsDegradedKey" does and is, naturally it came down to the OS was retrieving the image data and after it got it it was resizing it for quality output based on the settings and thats why I saw a second image.

So what I did was added PHImageRequestOptions parameters before the actual request for the asset and it retrieved only the photo i wanted:

...
PHAsset *lastAsset = [fetchResult lastObject];
PHImageRequestOptions * options = [[PHImageRequestOptions alloc] init];
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
options.synchronous = NO;
options.resizeMode = PHImageRequestOptionsVersionOriginal;
options.networkAccessAllowed = NO;
[[PHImageManager defaultManager] requestImageForAsset:lastAsset
                             targetSize:imageView.bounds.size
                             contentMode:PHImageContentModeAspectFit
                             options:options
                             resultHandler:^(UIImage *result, NSDictionary *info) {
                             ...