From c958493441edf622dbc320b9e1319298b8123481 Mon Sep 17 00:00:00 2001 From: maxaggedon Date: Wed, 14 Aug 2019 21:05:04 +0200 Subject: [PATCH] fix(lib): Convert PHAsset to JPEG for uploading --- ios/RNCAssetsLibraryRequestHandler.m | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/ios/RNCAssetsLibraryRequestHandler.m b/ios/RNCAssetsLibraryRequestHandler.m index ff083e668..8e1e88125 100644 --- a/ios/RNCAssetsLibraryRequestHandler.m +++ b/ios/RNCAssetsLibraryRequestHandler.m @@ -118,6 +118,38 @@ RCT_EXPORT_MODULE() } [delegate URLRequest:cancellationBlock didCompleteWithError:error]; }]; + } else if (isPHUpload) { + // By default, allow downloading images from iCloud + PHImageRequestOptions *const requestOptions = [PHImageRequestOptions new]; + requestOptions.networkAccessAllowed = YES; + requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat; + + CGSize const targetSize = CGSizeMake((CGFloat)asset.pixelWidth, (CGFloat)asset.pixelHeight); + [[PHImageManager defaultManager] requestImageForAsset:asset + targetSize:targetSize + contentMode:PHImageContentModeDefault + options:requestOptions + resultHandler:^(UIImage * _Nullable image, + NSDictionary * _Nullable info) { + NSError *const error = [info objectForKey:PHImageErrorKey]; + if (error) { + [delegate URLRequest:cancellationBlock didCompleteWithError:error]; + return; + } + + NSData *const imageData = UIImageJPEGRepresentation(image, 1.0); + NSInteger const length = [imageData length]; + + NSURLResponse *const response = [[NSURLResponse alloc] initWithURL:request.URL + MIMEType:@"image/jpeg" + expectedContentLength:length + textEncodingName:nil]; + + [delegate URLRequest:cancellationBlock didReceiveResponse:response]; + + [delegate URLRequest:cancellationBlock didReceiveData:imageData]; + [delegate URLRequest:cancellationBlock didCompleteWithError:nil]; + }]; } else { // By default, allow downloading images from iCloud PHImageRequestOptions *const requestOptions = [PHImageRequestOptions new];