从用户照片库中获取最后一张照片,返回两张照片
Posted
技术标签:
【中文标题】从用户照片库中获取最后一张照片,返回两张照片【英文标题】:Getting last picture taken from users photo library returning two pictures 【发布时间】:2015-09-07 01:54:12 【问题描述】:使用以下代码时,它会获取我的最后一张图片,但会复制它。它总是得到最后一张图片,但它成对返回:
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
这发生在模拟器和设备上。不使用第 3 方软件,没关系,我想。为什么会被调用两次?我在 uialertaction 块中调用它
【问题讨论】:
【参考方案1】:好吧,在对“PHImageResultIsDegradedKey”的作用和作用进行了一些研究之后,很自然地,它归结为操作系统正在检索图像数据,在它得到它之后,它正在根据设置调整它的大小以实现质量输出,这就是我看到的原因第二张图片。
所以我所做的是在实际请求资产之前添加 PHImageRequestOptions 参数,它只检索到我想要的照片:
...
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)
...
【讨论】:
【参考方案2】:将代码中的 PHAsset *lastAsset=[fetchResult lastObject];
替换为
PHAsset *lastAsset=fetchResult;
【讨论】:
以上是关于从用户照片库中获取最后一张照片,返回两张照片的主要内容,如果未能解决你的问题,请参考以下文章