Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Calling toImage() on a view with crashes on Android. How else to you get width/height from an Image?

Is this expected, that toImage() on a view is not supported on Android? According to the API docs (that we all know are half-baked) I didn't see anything that would lead me to believe that you cannot. My attempts to get an image blob on a View or ImageView all end badly. After banging my head a while against this I dug into the KitchenSink app and found an eventual call to toImage guarded by a check for the iPhone platform.

On iPhone, you can create an ImageView from a file and then call toImage to get an image blob on which you can query the width and height. Is there another way? Without such, do we really have to hard-code all our image dimensions to what the anticipated native dimensions of the image are?

— asked May 6th 2010 by Christopher Rumpf
  • android
  • blob
  • image
  • toimage
0 Comments

11 Answers

  • I found a reason why it was crashing for me. On Android, toImage() does not return a blob. Instead, it returns an object with basic info about the image, like x, y, width, height, and one of those properties is the blob (the media property). So an android, you need to do: view.toImage().media to get the Ti.Blob

    — answered April 30th 2012 by Christopher Stea
    permalink
    3 Comments
    • This worked for me!

      — commented January 17th 2013 by Freddy Montano
    • NICE! Thanks.

      — commented February 3rd 2013 by Miguel Ángel Castaño Fajardo
    • You, sir, are my hero. It's immensely annoying that they just "forget" to mention absolutely vital stuff like this in the documentation.

      Things like these, which should all be well documented, have already cost me upwards of 15 hours of useless "where's-this-bug-coming-from" time, which is huge cost for both my company and our end customers.

      — commented March 31st 2014 by Karst Ms
  • Have you tried setting the ImageView's width/height to "auto" ?

    — answered May 6th 2010 by Marshall Culpepper
    permalink
    1 Comment
    • That doesn't work either. When I set the ImgView's height to 'auto' and then query the height using imgView.size.height, it returns 0.

      — commented January 21st 2011 by Satheesh Subramanian
  • Hello Christopher,

    After the crash occurs, can you set the "Filter" on the "Test & Package" screen in Titanium Developer to "Trace", then copy all of the trace window's contents to clipboard and paste into a new "pastie" at pastie.org, then put the URL of your pastie here?

    Thank you,

    Bill

    — answered May 6th 2010 by Bill Dawson
    permalink
    0 Comments
  • Thanks guys for your answers:

    Here's the trace bit where things go down in flames:

    http://pastie.org/949366

    Indeed, it's complaining about width/height

    The offending code seems to be the toImage line in the code below:

    http://pastie.org/949372

    UPDATE

    @Bill_Dawson - I was hoping that your hunch was right. At first I did not have the view added to a window when I tried this. I changed things up so that the view was added to a window and then tried the toImage. Same result unfortunately. It's still before the window is actually opened though but I have to refactor a good number of things before I can test that out.

    — answered May 6th 2010 by Christopher Rumpf
    permalink
    0 Comments
  • Ok, the problem I THINK is that the toImage() can't really be processed until the view is really "on the canvas", so to speak. You haven't added it yet to a window, so it hasn't been rendered, therefore no image snapshot can be taken of it. That's my theory… can you try what it happens if you add the imageView first to the window then check to see if you can .toImage() it?

    — answered May 6th 2010 by Bill Dawson
    permalink
    0 Comments
  • Hi guys,
    I'm having the same problem I can't get the image dimensions on the Android.
    Any suggestions?

    — answered June 3rd 2010 by Daniele Sangalli
    permalink
    1 Comment
    • Hey Daniele,
      I never found a solution. As much as it pained me, I just hard-coded my image dimensions. /sigh

      — commented June 4th 2010 by Christopher Rumpf
  • still no solution for android? Hardcoding is not a good solution….

    — answered July 23rd 2010 by daniel otto
    permalink
    2 Comments
    • sorry, i solved the problem with this solution:

      http://developer.appcelerator.com/question/33801/position-vertical-labels

      — commented July 23rd 2010 by daniel otto
    • Have you used the size property of the view?

      — commented July 23rd 2010 by Don Thorp
  • 9 months later and still no solution.

    common, i think this would be worth resolving, no?

    setting the width to auto is no good, cause it just sets the width to something like the width of the parent.
    but as there isn't a horizontal layout in android, yet, there is no way to position dynamic elements side by side. also there is no way to center dynamic objects inside a view…

    toImage() crashes on android, and the size property has the value of 0.

    — answered January 11th 2011 by marcin kolonko
    permalink
    0 Comments
  • I think you can use toBlob() instead to toImage() which returns the image as a Blob object.
    Hope it help.

    — answered August 27th 2012 by Xiang Wei Lee
    permalink
    0 Comments
  • For my Android 2.2, the view.toImage() indeed did not result in an image file. Following the suggestion by Christopher Stea I could solve this by adding .media. So you simply use view.toImage().media, and you will see the problem is solved.

    — answered December 13th 2012 by Eric Kirchner
    permalink
    0 Comments
  • What someone said earlier is absolutely correct. You can't run a toImage on an Android view that hasn't actually been added to the screen yet. My work around is creating a view "dump receptacle" that I place at the top of the screen or view somewhere at top: 0 - 1000. That way you can use it as a desk of sorts to process the image. Then the temporary view can be removed once the new view is ready to be placed to the screen.

    — answered December 22nd 2012 by Francis Meetze
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.