Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ private File resizedImage(String path, Double maxWidth, Double maxHeight) throws

Bitmap scaledBmp = Bitmap.createScaledBitmap(bmp, width.intValue(), height.intValue(), false);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
scaledBmp.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
boolean saveAsPNG = bmp.hasAlpha();
scaledBmp.compress(
saveAsPNG ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG, 100, outputStream);

String[] pathParts = path.split("/");
String imageName = pathParts[pathParts.length - 1];
Expand Down
14 changes: 12 additions & 2 deletions packages/image_picker/ios/Classes/ImagePickerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,12 @@ - (void)imagePickerController:(UIImagePickerController *)picker
image = [self scaledImage:image maxWidth:maxWidth maxHeight:maxHeight];
}

NSData *data = UIImageJPEGRepresentation(image, 1.0);
BOOL saveAsPNG = [self hasAlpha:image];
NSData *data =
saveAsPNG ? UIImagePNGRepresentation(image) : UIImageJPEGRepresentation(image, 1.0);
NSString *fileExtension = saveAsPNG ? @"image_picker_%@.png" : @"image_picker_%@.jpg";
NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString];
NSString *tmpFile = [NSString stringWithFormat:@"image_picker_%@.jpg", guid];
NSString *tmpFile = [NSString stringWithFormat:fileExtension, guid];
NSString *tmpDirectory = NSTemporaryDirectory();
NSString *tmpPath = [tmpDirectory stringByAppendingPathComponent:tmpFile];

Expand Down Expand Up @@ -254,4 +257,11 @@ - (UIImage *)scaledImage:(UIImage *)image
return scaledImage;
}

// Returns true if the image has an alpha layer
- (BOOL)hasAlpha:(UIImage *)image {
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image.CGImage);
return (alpha == kCGImageAlphaFirst || alpha == kCGImageAlphaLast ||
alpha == kCGImageAlphaPremultipliedFirst || alpha == kCGImageAlphaPremultipliedLast);
}

@end