[TIMOB-15247] iOS: Ti.Blob.imageAsResized always returns a png image
| GitHub Issue | n/a |
|---|---|
| Type | Bug |
| Priority | High |
| Status | Closed |
| Resolution | Fixed |
| Resolution Date | 2015-04-16T18:21:32.000+0000 |
| Affected Version/s | n/a |
| Fix Version/s | n/a |
| Components | iOS |
| Labels | blob, image, jpeg, parity, png |
| Reporter | Cedric Paternotte |
| Assignee | Vishal Duggal |
| Created | 2013-07-05T16:05:43.000+0000 |
| Updated | 2017-03-31T22:22:06.000+0000 |
A simple test case would be nice :)
Here is a workaround that works for me on iOS : use Ti.ImageFactory.compress to force jpeg format. This workaround requires the [ImageFactory](https://marketplace.appcelerator.com/apps/4973) module ([github](https://github.com/appcelerator/titanium_modules/tree/master/imagefactory)).
// read source image var sourceImage = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, 'source.jpg').read(); // resize it using core Ti.Blob.imageAsResized() var resizedImage = sourceImage.imageAsResized(160,120); // do this step if ios only if (OS_IOS) { // choose desired compression level (from 0.0 to 1.0) var compression_level = 0.75; // load ImageFactory module var ImageFactory = require('ti.imagefactory'); // convert image to jpeg resizedImage = ImageFactory.compress( resizedImage, compression_level ); } // create destination file var destFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'destination.jpg'); // write resized image to destination file destFile.write(resizedImage);This issue requires a Test Case.
Test case:
var image = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, 'srcImage.jpg').read(); Ti.API.info("original image size:" + image.size); Ti.API.info("original image dimensions: " + image.width + 'x' + image.height); var newBlob1 = image.imageAsResized(160,120); Ti.API.info("test 1 image size:" + newBlob1.size); Ti.API.info("test 1 image dimensions: " + newBlob1.width + 'x' + newBlob1.height); var bgImage = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'test.jpg'); bgImage.write(newBlob1); // // => Even though the output filename contains .jpg, it's a png file that is created (but named 'test.jpg') // // There's two ways to verify that it's a png file that has been created: // 1) the file is large (much larger than if it had been saved as jpeg) // 2) viewing the file with an "advanced" image viewer (like, for instance, IrfanView on Windows), one can check that the filetype is actually png rather than jpg //Hi, I solved this issue. The problem lies in the UIImage+Resize.m When the image is resized in the + (UIImage *)resizedImage:(CGSize)newSize transform:(CGAffineTransform)transform drawTransposed:(BOOL)transpose interpolationQuality:(CGInterpolationQuality)quality image:(UIImage*)image hires:(BOOL)hires it is always assumed that the image has an alpha channel by passing kCGImageAlphaPremultipliedLast when the bitmap context is created. The passed image has first to be checked if it has an alpha channel or not and then create the bitmap context accordingly. Long story short: replace kCGImageAlphaPremultipliedLast with [UIImageAlpha hasAlpha:image] ? kCGImageAlphaPremultipliedLast : kCGImageAlphaNoneSkipFirst at line 49 of this file. Tested and works on Ti 3.2.2.GA, iOS 7.1, simualtor iPhone 3.5''
Android seems to be returning a bitmap. "image/bitmap" mimetype. Can anything be done about this?
Thank you all. Is anyone able to file a PR based on these findings? If so, we'd be happy to merge it in.
I'm going to try out [~giorgos29cm]'s suggestion & will update my results.
For me [~lulu]'s use of *ImageFactory* has been the best option. I did not see a difference on iOS with [~giorgos29cm] modification. Someone can double check. Result on *Android* - image.imageAsResized...): 1- image Original size: undefined image Original dimensions: 3264x2448 2- image size Resize :undefined image dimensions After Resize: 853x640 3- image JPG image size: 58551 (very decent for a Camera Snapshot on a Samsung S3) On *iOS* (different Image with ImageFactory.compress(image.imageAsResized...), 0.75): 1- image Original size: 1500000 image Original dimensions: 1500x1000 2- image size Resize: 614400 image dimensions After Resize: 640x960 3- image JPG image size: 233242
Cedric's workaround has worked for me. I don't have the stats handy, but pre-workaround, resized images were around 2MB and I confirmed were PNG data in a blob otherwise identified as a JPG. Post-workaround, sizes are around 150KB w/JPG data in the blob. I have not tried Giorgos' modification.
Pull pending master - https://github.com/appcelerator/titanium_mobile/pull/6783
the PR above was merged 16 Apr, but with 4.0.0.GA I still have a png on iOS when just using imageAsResized
Closing ticket as fixed, if there are any problems, please file a new ticket.