Problem description
The method toImage() does not work if applied to an ImageView. This is also true if the ImageView is child of another view.
Steps to reproduce
Use the following code to reproduce the issue:
var win = Ti.UI.createWindow({backgroundColor: 'white'});
var img = Ti.UI.createImageView({
image: 'KS_nav_views.png',
backgroundColor: 'transparent',
width: 100,
height: 100
});
var tmpImage = img.toImage();
tmpImage.backgroundColor = 'transparent';
var imageView = Titanium.UI.createImageView({
width: 100,
height: 100,
image: tmpImage,
top: 10,
backgroundColor: "transparent"
});
win.add(imageView);
win.open();
Additional notes
The same code works with SDK 3.0.0.
If the ImageView is child of another view, and the toImage() is applied to the parent, the ImageView will not be visible as well.
Ran the test case on 3.0.0.GA and 2.1.4.GA. On 3.0.0.GA, no image showed on the screen. On 2.1.4.GA, a black square showed on the screen. Seems the test case never worked.
@Ping I've updated the test code, now I can replicate. The first image was missing width and height.
There is a behavior change in imageview in 3.0.2. Before 3.0.2, if the image is local, it will be loaded synchronously; if the image is remote, it will be loaded asynchronously. After 3.0.2, all the images are loaded asynchronously no matter local images or remote images. The toImage() method is to give a snapshot of the view. Therefore, if the image is loaded asynchronously, the snapshot will be empty because the image is not ready yet. The above test case works in 3.0.0 because it uses a local image. If we modify Line 4 to
it will not work even in 3.0.0.
Here are two workarounds for this issue: 1. Set the "backgroundImage" or "defaultImage" property to the imageview. Sample code:
2. Add listener to the ["load"](http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.ImageView-event-load) event. In this case we are sure that the toImage() method is called after the image finishes loading. *Note:* in order to fire the "load" event, the imageview must be added to the window. Sample code:
People are watching this ticket. Closing an issue people are interested in a resolution of as Resolved/Won't Fix without comment leaves people hanging and deteriorates hope in the platform. Could you provide a short explanation of why this won't be fixed? At the very least the documentation should be updated to explain the asynchronous nature of the "image" property and the available workarounds.
Closing ticket as "Won't Fix". There has been no update for quite a while. If there is any problem, please open a new ticket.