Clearing the image from an Image view
I need to reset the image property on an imageview and seem to be running into a problem where the new image stacks ontop of the new one. This is when using the image property not the url, is there a way to clear the image property? I've tried setting it to null, blank, undefined, etc. Nothing seems to work, even removing it from the parent and adding again retails the old image.
4 Answers
-
You should be able to dynamically change the image via
imageView.url = '../images/whatever.png';
. There's an example of this in the current Kitchen Sink. -
Not exactly, i'm using the imageview image property
image:theImage
pulling from a photo picker or camera, so the url property isn't helping, any other ideas?
-
Hi Christopher,
Today I finally worked out a solution for a similar problem. Because the imageview is acumulating new images (stacking new images on top of older ones) it has to be reset before a new image is added to the imageview. So I did the following:
I changed:
var filename = 'myImage.png'; savedfile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename); var imageView = Titanium.UI.createImageView({width:300}); imageView.image = savedfile;
into:
var filename = 'myImage.png'; savedfile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename) var imageView = Titanium.UI.createImageView({width:300}); imageView.url = '../images/transp.png'; imageView.image = savedfile;
Where the line: - imageView.url = '../images/transp.png'; - connects to an .png image which is 10 X 10 px and completely transparent.
So before you set the image property of the imageview, you "empty" it by first overwriting the image property with a transparent .png file. After that you can set the image property to another image…even semitransparent ones.
Good luck!
-
Set the property to null.
In my project, I set the properties on a object by classing them in another object, called by a function, for example, I have an object:
stdLabel : { left: '2%'', height: '5%' }
I call a function to create a label as so:
var myLabel = createLabel('stdLabel')
This returns a label variable created through Ti.UI.createLabel(this[className])Bascially, I had a problem on orientation change that a right property I had set on the landscape version needed to be removed when switching back to portrait:
Login_vehicleDDL : { obsRestyle : true, top : '45%', width : 'auto' }, Login_vehicleDDL_land : { obsRestyle : true, top : '60%', right : '2%', width : '48%' },
Note no right property on the first class.
The restyling event and the function I wrote does the work of resetting all the properties on a UI object (in this case the DDL) to the values in the class object, but as there's no property right in the class, it doesn't get reset, leaving me with a malaligned DDL
Solution:
Login_vehicleDDL : { obsRestyle : true, top : '45%', right : null, width : 'auto' }, Login_vehicleDDL_land : { obsRestyle : true, top : '60%', right : '2%', width : '48%' },
Adding the null value to the property sets the property of the label to null, which is then ignored by the titanium formatter.
What a pain to find, but a very simple solution.