Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-15247] iOS: Ti.Blob.imageAsResized always returns a png image

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2015-04-16T18:21:32.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsiOS
Labelsblob, image, jpeg, parity, png
ReporterCedric Paternotte
AssigneeVishal Duggal
Created2013-07-05T16:05:43.000+0000
Updated2017-03-31T22:22:06.000+0000

Description

Comments

  1. Daniel Sefton 2013-07-12

    A simple test case would be nice :)
  2. Cedric Paternotte 2013-07-12

  3. Cedric Paternotte 2013-07-12

    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);
       
  4. Mauro Parra-Miranda 2013-09-09

    This issue requires a Test Case.
  5. Cedric Paternotte 2013-09-10

    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
       //
       
  6. Giorgos Papadopoulos 2014-04-13

    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''
  7. Paul Hamilton 2014-08-04

    Android seems to be returning a bitmap. "image/bitmap" mimetype. Can anything be done about this?
  8. Ingo Muschenetz 2015-04-06

    Thank you all. Is anyone able to file a PR based on these findings? If so, we'd be happy to merge it in.
  9. Joseph Sachs 2015-04-13

    I'm going to try out [~giorgos29cm]'s suggestion & will update my results.
  10. Joseph Sachs 2015-04-15

    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
  11. Tim Poulsen 2015-04-15

    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.
  12. Vishal Duggal 2015-04-15

    Pull pending master - https://github.com/appcelerator/titanium_mobile/pull/6783
  13. Michael Gangolf 2015-06-29

    the PR above was merged 16 Apr, but with 4.0.0.GA I still have a png on iOS when just using imageAsResized
  14. Lee Morris 2017-03-31

    Closing ticket as fixed, if there are any problems, please file a new ticket.

JSON Source