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?
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
Have you tried setting the ImageView's width/height to "auto" ?
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?
Thanks guys for your answers:
Here's the trace bit where things go down in flames:
Indeed, it's complaining about width/height
The offending code seems to be the toImage line in the code below:
@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.
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?
I'm having the same problem I can't get the image dimensions on the Android.
still no solution for android? Hardcoding is not a good solution….
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.
I think you can use toBlob() instead to toImage() which returns the image as a Blob object.
Hope it help.
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.
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.