iOS 8 PhotoKit.从iCloud照片共享相册中获取最大大小的图像



如何从iСloud访问全尺寸图像?每次我试着拍这张照片时,我都会得到256x342的图像。我也看不到进展。

代码:

    PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetIdentifier] options:nil];
    PHImageManager *manager = [PHImageManager defaultManager];
    [result enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        options.synchronous = YES;
        options.networkAccessAllowed = YES;
        options.progressHandler = ^(double progress, NSError *error, BOOL *stop, NSDictionary *info) {
            NSLog(@"%f", progress);
        };
        [manager requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage *resultImage, NSDictionary *info)
         {
             UIImage *image = resultImage;
             NSLog(@"%@", NSStringFromCGSize(resultImage.size));
         }];
    }];

在我点击照片应用程序中的图片之前,这张图片的质量会很差。但我一点击图片,它就会下载到设备上,并且是全尺寸的质量。

我认为下面应该得到全分辨率的图像数据:

 [manager requestImageDataForAsset:asset 
                           options:options 
                     resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) 
             { 
                  UIImage *image = [UIImage imageWithData:imageData]; 
                  //...
             }];

WWDC视频涵盖了整个照片框架(PhotoKit):https://developer.apple.com/videos/wwdc/2014/#511

希望这能有所帮助。

编辑:

resultHandler可以调用两次。我在30:00左右链接到的视频中对此进行了解释。可能是你只得到了缩略图,第二次调用时会出现完整的图像。

我遇到了一些相同的问题。这要么是一个错误,要么是糟糕的文档。我已经能够通过指定2000x2000的请求大小来解决这个问题。问题是,我确实得到了全尺寸的图像,但有时它会被标记为降级,所以我一直在等待不同的图像,这从未发生过。这就是我为解决这些问题所做的。

        self.selectedAsset = asset;
        self.collectionView.allowsSelection = NO;
        PHImageRequestOptions* options = [[[PHImageRequestOptions alloc] init] autorelease];
        options.synchronous = NO;
        options.version = PHImageRequestOptionsVersionCurrent;
        options.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;
        options.resizeMode = PHImageRequestOptionsResizeModeNone;
        options.networkAccessAllowed = YES;
        options.progressHandler =  ^(double progress,NSError *error,BOOL* stop, NSDictionary* dict) {
            NSLog(@"progress %lf",progress);  //never gets called
        };
        [self.delegate choosePhotoCollectionVCIsGettingPhoto:YES];  //show activity indicator
        __block BOOL isStillLookingForPhoto = YES;
        currentImageRequestId = [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(2000, 2000) contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage *result, NSDictionary *info) {
            NSLog(@"result size:%@",NSStringFromCGSize(result.size));
            BOOL isRealDealForSure = NO;
            NSNumber* n = info[@"PHImageResultIsPlaceholderKey"]; //undocumented key so I don't count on it
            if (n != nil && [n boolValue] == NO){
                isRealDealForSure = YES;
            }
            if([info[PHImageResultIsInCloudKey] boolValue]){
                NSLog(@"image is in the cloud"); //never seen this. (because I allowed network access)
            }
            else if([info[PHImageResultIsDegradedKey] boolValue] && !isRealDealForSure){
                    //do something with the small image...but keep waiting
                [self.delegate choosePhotoCollectionVCPreviewSmallPhoto:result];
                self.collectionView.allowsSelection = YES;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ //random time of 3 seconds to get the full resolution in case the degraded key is lying to me. The user can move on but we will keep waiting.
                    if(isStillLookingForPhoto){
                        self.selectedImage = result;
                        [self.delegate choosePhotoCollectionVCPreviewFullPhoto:self.selectedImage]; //remove activity indicator and let the user move on
                    }
                });
            }
            else {
                    //do something with the full result and get rid of activity indicator.
                if(asset == self.selectedAsset){
                    isStillLookingForPhoto = NO;
                    self.selectedImage = result;
                    [self.delegate choosePhotoCollectionVCPreviewFullPhoto:self.selectedImage];
                    self.collectionView.allowsSelection = YES;
                }
                else {
                    NSLog(@"ignored asset because another was pressed");
                }
            }
        }];

要获得全尺寸图像,您需要查看信息列表。我用它来测试返回的结果是完整的图像,还是降级的版本。

if ([[info valueForKey:@"PHImageResultIsDegradedKey"]integerValue]==0){
    // Do something with the FULL SIZED image
} else {
    // Do something with the regraded image
}

或者你可以用这个来检查你是否得到了你想要的东西。

if ([[info valueForKey:@"PHImageResultWantedImageFormatKey"]integerValue]==[[info valueForKey:@"PHImageResultDeliveredImageFormatKey"]integerValue]){
    // Do something with the FULL SIZED image
} else {
    // Do something with the regraded image
}

还有许多其他未记录但有用的密钥,例如

PHImageFileOrientationKey = 3;
PHImageFileSandboxExtensionTokenKey = "/private/var/mobile/Media/DCIM/100APPLE/IMG_0780.JPG";
PHImageFileURLKey = "file:///var/mobile/Media/DCIM/100APPLE/IMG_0780.JPG";
PHImageFileUTIKey = "public.jpeg";
PHImageResultDeliveredImageFormatKey = 9999;
PHImageResultIsDegradedKey = 0;
PHImageResultIsInCloudKey = 0;
PHImageResultIsPlaceholderKey = 0;
PHImageResultRequestIDKey = 1;
PHImageResultWantedImageFormatKey = 9999;

玩得开心。直线

我相信这与您设置PHImageRequestOptionsDeliveryModeOpportunitic有关。请注意,异步模式(默认)甚至不支持此功能。请尝试PHImageRequestOptionsDeliveryModeHighQualityFormat插件。

最新更新