简体   繁体   English

iOS 8 PhotoKit。 从iCloud照片共享相册中获取最大尺寸的图像

[英]iOS 8 PhotoKit. Get maximum-size image from iCloud Photo Sharing albums

How get access to the full-size images from iСloud? 如何从iСloud访问全尺寸图像? Every time I try to get this picture, I get image size 256x342. 每次我尝试拍摄这张照片时,我都会得到256x342的图像尺寸。 I not see progress too. 我也没有看到进展。

Code: 码:

    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));
         }];
    }];

Until I click the picture in Photo app, this picture will be of poor quality. 直到我点击照片应用程序中的图片,这张图片质量很差。 But as soon as I click on the picture, it downloaded on the device and will be full-size quality. 但是一旦我点击图片,它就会在设备上下载,并且质量非常全。

I think the below should get the full resolution image data: 我认为下面应该得到全分辨率图像数据:

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

                  //...

             }];

The entire Photos Framework (PhotoKit) is covered in the WWDC video: https://developer.apple.com/videos/wwdc/2014/#511 整个照片框架(PhotoKit)包含在WWDC视频中: https//developer.apple.com/videos/wwdc/2014/#511

Hope this helps. 希望这可以帮助。

Edit: 编辑:

The resultHandler can be called twice. resultHandler可以被调用两次。 This is explained in the video I linked to at around 30:00. 我在30:00左右链接到的视频中对此进行了解释。 Could be that you are only getting the thumbnail and the full image will come with the second time its called. 可能是你只获得了缩略图,并且第二次调用完整的图像。

I'm having some of the same issues. 我有一些相同的问题。 It is either a bug or poor documentation. 这是一个错误或糟糕的文档。 I've been able to get around the issue by specifying a requested size of 2000x2000. 通过指定2000x2000的请求大小,我已经能够解决这个问题。 The problem with this is that I do get the full size image but sometimes it comes back marked as degraded so I keep waiting for a different image which never happens. 这个问题是我确实获得了完整尺寸的图像,但有时它会被标记为降级,所以我一直在等待从未发生的不同图像。 This is what I do to get around those issues. 这就是我解决这些问题的方法。

        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");
                }
            }
        }];

To get the full size image you need to check the info list. 要获得完整尺寸的图像,您需要检查信息列表。 I used this to test if the returned result is the full image, or a degraded version. 我用它来测试返回的结果是完整图像还是降级版本。

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

or you could use this to check if you got back what you asked for. 或者你可以用它来检查你是否找回了你要求的东西。

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

There are a number of other, undocumented but useful, keys eg 还有许多其他未记载但有用的密钥,例如

 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; 

Have fun. 玩得开心。 Linasses Linasses

I believe it's related to you setting PHImageRequestOptionsDeliveryModeOpportunistic. 我相信这与您设置PHImageRequestOptionsDeliveryModeOpportunistic有关。 Note that this is not even supported for asynchronous mode (default). 请注意,异步模式甚至不支持此功能(默认)。 Try PHImageRequestOptionsDeliveryModeHighQualityFormat intead. 尝试PHImageRequestOptionsDeliveryModeHighQualityFormat intead。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM