2

我的公司在通过获取 PHAsset 获取正确大小的元数据方面遇到了很大的问题。我们开发了一个 iOS 应用程序,允许客户从库中选择图片,获取每个图片的大小(以像素为单位),计算坐标以调整我们销售的小工具,然后将高质量的图片版本上传到我们的服务器以打印小工具。对于我们的一些客户,问题是发送的一些高质量版本的图片的像素大小与 PHAsset 对象给出的 pixelWidth 和 pixelHeight 不匹配。举个例子,我们有一张图片:

  • PHAsset 对象报告为 2096x3724
  • 但是,当请求全尺寸图像时,会生成 1536x2730 的图片

图片不在 iCloud 中,由运行 iOS 10.2 的 iPhone 6 SE 发送。这是获取全尺寸图像版本的代码:

PHImageRequestOptions *imgOpts = [[PHImageRequestOptions alloc] init];
imgOpts.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
imgOpts.networkAccessAllowed = YES;
imgOpts.resizeMode = PHImageRequestOptionsResizeModeExact;
imgOpts.version = PHImageRequestOptionsVersionCurrent;  

PHCachingImageManager *imageManager = [[PHCachingImageManager alloc] init];

[imageManager requestImageForAsset:imageAsset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:imgOpts resultHandler:^(UIImage *  result, NSDictionary *  info) {
    NSData * imageData = UIImageJPEGRepresentation(result, 0.92f);
    //UPLOAD OF imageData TO SERVER HERE
}]

也尝试使用 requestImageDataForAsset 方法,但没有运气:

PHImageRequestOptions *imgOpts = [[PHImageRequestOptions alloc] init];
imgOpts.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
imgOpts.networkAccessAllowed = YES;
imgOpts.resizeMode = PHImageRequestOptionsResizeModeExact;
imgOpts.version = PHImageRequestOptionsVersionCurrent;  

PHCachingImageManager *imageManager = [[PHCachingImageManager alloc] init];

[imageManager requestImageDataForAsset:imageAsset options:imgOpts resultHandler:^(NSData * imageData, NSString * dataUTI, UIImageOrientation orientation, NSDictionary *  info) {
    //UPLOAD OF imageData TO SERVER HERE
}]

在上传之前,从每张图片的高分辨率版本中获取精确尺寸对我们来说不是一个选项,因为从库中选择大量资产时会降低很多性能。

我们错过了还是做错了什么?有没有办法在不将全分辨率图像加载到内存的情况下以像素为单位获取资产大小?感谢您的帮助

4

2 回答 2

1

这是由于Photos框架中的错误。可以在此处找到有关该错误的详细信息。

有时,在编辑照片后,会创建一个较小的版本。这只发生在一些较大的照片上。

当尝试检索已编辑的版本 ( ) 时,调用requestImageForAsset:(with PHImageManagerMaximumSize) 或requestImageDataForAsset:(with PHImageRequestOptionsDeliveryModeHighQualityFormat) 将从较小的文件版本中读取数据PHImageRequestOptionsVersionCurrent

上述info方法的回调中会指向图片的路径。举个例子:
PHImageFileURLKey = "file:///[...]DCIM/100APPLE/IMG_0006/Adjustments/IMG_0006.JPG";
检查那个文件夹,我找到了另一张图片FullSizeRender.jpg——这张图片是全尺寸的,并且包含最新的编辑。FullSizeRender.jpg因此,一种方法是在存在此类文件时尝试从.


从 iOS 9 开始,还可以使用以下命令以最高分辨率获取最新编辑PHAssetResourceManager

// if (@available(iOS 9.0, *)) {
// check if a high quality edit is available
NSArray<PHAssetResource *> *resources = [PHAssetResource assetResourcesForAsset:_asset];
PHAssetResource *hqResource = nil;
for (PHAssetResource *res in resources) {
    if (res.type == PHAssetResourceTypeFullSizePhoto) {
        // from my tests so far, this is only present for edited photos
        hqResource = res;
        break;
    }
}

if (hqResource) {
    PHAssetResourceRequestOptions *options = [[PHAssetResourceRequestOptions alloc] init];
    options.networkAccessAllowed = YES;
    long long fileSize = [[hqResource valueForKey:@"fileSize"] longLongValue];
    NSMutableData *fullData = [[NSMutableData alloc] initWithCapacity:fileSize];

    [[PHAssetResourceManager defaultManager] requestDataForAssetResource:hqResource options:options dataReceivedHandler:^(NSData * _Nonnull data) {
        // append the data that we're receiving
        [fullData appendData:data];
    } completionHandler:^(NSError * _Nullable error) {
        // handle completion, using `fullData` or `error`
        // uti == hqResource.uniformTypeIdentifier
        // orientation == UIImageOrientationUp
    }];
}
else {
    // use `requestImageDataForAsset:`, `requestImageForAsset:` or `requestDataForAssetResource:` with a different `PHAssetResource`
}
于 2018-12-28T15:06:39.003 回答
-1

你能试试这个来获取相机胶卷照片吗:

__weak __typeof(self) weakSelf = self;
PHFetchResult<PHAssetCollection *> *albums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumSelfPortraits options:nil];
[albums enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull album, NSUInteger idx, BOOL * _Nonnull stop) {
    PHFetchOptions *options = [[PHFetchOptions alloc] init];
    options.wantsIncrementalChangeDetails = YES;
    options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %d",PHAssetMediaTypeImage];
    options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
    PHFetchResult<PHAsset *> *assets = [PHAsset fetchAssetsInAssetCollection:album options:options];
    if(assets.count>0)
    {
        [assets enumerateObjectsUsingBlock:^(PHAsset * _Nonnull asset, NSUInteger idx, BOOL * _Nonnull stop) {
            if(asset!=nil)
            {
                [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage *result, NSDictionary *info)
                 {
                     dispatch_async(dispatch_get_main_queue(), ^{
                         [weakSelf addlocalNotificationForFilters:result];
                         // [weakSelf.buttonGalery setImage:result forState:UIControlStateNormal];
                     });
                 }];
                *stop = YES;
            }
            else{
                [weakSelf getlatestAferSelfie];
            }
        }];
    }
于 2017-04-24T13:36:45.740 回答