使用以下代码时,它会获取我的最后一张照片,但它会复制它。它总是得到最后一张图片,但它会成对返回:
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
这发生在模拟器&amp;在设备上。我认为,不使用第三方软件,这不重要。它为什么被叫两次?我在一个uialertaction块中称它为
答案 0 :(得分:0)
做了一些关于&#34; PHImageResultIsDegradedKey&#34;是的,当然,它归结为操作系统检索图像数据,并在它得到它之后根据设置调整质量输出的大小,这就是为什么我看到第二个图像。
所以我所做的是在资产的实际请求之前添加了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) {
...
答案 1 :(得分:0)
仅使用
替换代码中的PHAsset *lastAsset=fetchResult;
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/spring.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>