Skip to content

Commit

Permalink
Update how we request images so we don’t just convert UIImage to JPEG…
Browse files Browse the repository at this point in the history
… representations
  • Loading branch information
ScottPetit committed Jun 8, 2020
1 parent e3ca720 commit ff115af
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions HSAttachmentPicker/HSAttachmentPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,24 @@ - (void)uploadMovie:(NSDictionary<UIImagePickerControllerInfoKey, id> * _Nonnull
[self upload:videoData filename:filename image:nil];
}

- (void)uploadPhoto:(PHAsset*)photo {
- (void)uploadPhoto:(PHAsset *)photo {
PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
CGSize targetSize = photo.pixelWidth > photo.pixelHeight ? CGSizeMake(1024, 768) : CGSizeMake(768, 1024);
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
requestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
requestOptions.synchronous = YES;
[PHImageManager.defaultManager requestImageForAsset:photo targetSize:targetSize contentMode:PHImageContentModeAspectFit options:requestOptions resultHandler:^(UIImage *result, NSDictionary *info) {
NSData *data = UIImageJPEGRepresentation(result, 0.5);
NSString *filename = [photo valueForKey:@"filename"] ?: @"photo.jpg";
[self upload:data filename:filename.lowercaseString image:result];
}];
if (@available(iOS 13.0, *)) {
[PHImageManager.defaultManager requestImageDataAndOrientationForAsset:photo options:requestOptions resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, CGImagePropertyOrientation orientation, NSDictionary * _Nullable info) {
UIImage *image = [UIImage imageWithData:imageData];
NSString *filename = [photo valueForKey:@"filename"] ?: @"photo.jpg";
[self upload:imageData filename:filename.lowercaseString image:image];
}];
} else {
[PHImageManager.defaultManager requestImageDataForAsset:photo options:requestOptions resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
UIImage *image = [UIImage imageWithData:imageData];
NSString *filename = [photo valueForKey:@"filename"] ?: @"photo.jpg";
[self upload:imageData filename:filename.lowercaseString image:image];
}];
}
}

#pragma mark - HSAttachmentPickerPhotoPreviewControllerDelegate
Expand Down

0 comments on commit ff115af

Please sign in to comment.