无法使用PhotoKit从相册中获取清晰的图像

时间:2015-08-14 10:52:14

标签: ios uiimageview uiimage photokit

我正在使用PhotoKit来获取其中一个系统相册中的照片:

PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
[fetchResult enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
    NSLog(@"ALBUM NAME: %@", collection.localizedTitle);
    if ([collection.localizedTitle isEqualToString:@"Camera Roll"]) {
        PHFetchResult *photos = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
        NSLog(@"PHOTOS: %ld", photos.count);
        _photos = nil;
        _photos = @[].mutableCopy;
        for (PHAsset *asset in photos) {
            [self loadImageFromPHAsset:asset];
        }
    }
}];

然后在我的自定义助手方法中:(void)loadImageFromPHAsset:(PHAsset *)asset,我有:

-(void)loadImageFromPHAsset:(PHAsset *)asset
{
    PHImageManager *manager = [PHImageManager defaultManager];
    CGSize targetSize = _layout.itemSize;
    [manager requestImageForAsset:asset targetSize:targetSize contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage *result, NSDictionary *info) {
        [_photos addObject:result];
    }];
    [self.collectionView reloadData];
}

所以我有一系列图片,我将它们呈现在UICollectionViewCell

UIImage *image = [_photos objectAtIndex:indexPath.row];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.image = image;

但我得到的是这样,它非常模糊,我怎么能说清楚? enter image description here

1 个答案:

答案 0 :(得分:0)

首先,targetSize应该乘以比例,以便像这样考虑屏幕的比例,

CGFloat scale = [UIScreen mainScreen].scale;
CGSize targetSize = CGSizeMake(_layout.itemSize.width*scale, _layout.itemSize.height*scale);

其次,尝试使用这些选项来获得精确尺寸的图像,

//Async call returned on main thread, can return multiple times

PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeExact;

最后,如果您有使用PHCachingManager的性能问题,我已经回答了有关性能改进的问题here

希望这会对你有所帮助。

修改

我没有意识到图像存储在一个数组中。尝试在图像选项中设置同步标记。

PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = YES;
options.resizeMode = PHImageRequestOptionsResizeModeExact;