imageSource problem

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 346
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

imageSource problem

Post by thatkeith » Tue Nov 06, 2018 12:20 am

I'm hitting some frustrating behaviour, I hope someone can help me eliminate it.

I want to set a number of different images as the imageSource for characters in a scrolling field, one per line. When I run my script to do more than one image (with a repeat loop going through my list of file paths) what keeps happening is the latest image in the loop is shown in EVERY imageSource character – replacing the different images set in an earlier loop of the repeat.

Anyone know how I can get this to show different images, one per line?

I can do this if I run each 'set the imageSource' entirely separately, but that's rather missing the point. :)

Code: Select all

   -- 'it' contains a number of file paths, one per line.
   
   repeat with x = 1 to the number of lines of it
      -- (1) import an image,
      -- (2) scale it,
      -- (3) throw away the 'extra' image data, then
      -- (4) set the imageData of a char to the imported image
      
      -- (1)
      set the filename of image "thumb" to line x of it
      
      -- (2)
      set the width of image "thumb" to 720
      if the width of image "thumb" > the height of image "thumb" * 6 then
         set the height of image "thumb" to 60
      else
         set the height of image "thumb" to 120
      end if
      
       -- (3)
      set the imageData of image "thumb" to the imageData of image "thumb"
      
       -- (4)
      set the imageSource of char 1 of line x of field "test" to "thumb"
      
   end repeat
It's the last line (set the imageSource) that goes wrong; each existing imageSource character is changed to the imageData used to set up the character in the new line, so I end up with a scrolling field with multiple copies of only the last image in the list. :-/

k
Technical Writer, Meta
University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: imageSource problem

Post by jmburnod » Tue Nov 06, 2018 9:53 am

Hi thatkeith,
As far I know imagesource need an image to work.
I think you have to create an image for each imagesource in your loop,
something like that (no tested):

Code: Select all

repeat with x = 1 to the number of lines of it
   -- (1)
   put "thumb" & x into tCurThumb
   new img tCurThumb
   set the filename of image tCurThumb to line x of it
   -- (2)
   set the width of image tCurThumbto 720
   if the width of image tCurThumb > the height of image tCurThumb * 6 then
      set the height of image tCurThumb to 60
   else
      set the height of image tCurThumb to 120
   end if
   -- (3)
   set the imageData of image tCurThumb to the imageData of image tCurThumb
   -- (4)
   set the imageSource of char 1 of line x of field "test" to tCurThumb
end repeat
Best
Jean-Marc
https://alternatic.ch

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 346
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: imageSource problem

Post by thatkeith » Tue Nov 06, 2018 10:31 am

I have one image object in my card, and I use that as a temporary store for importing and resizing each original photo. So... I think I understand you, and hence the problem. Do you mean that each imageSource character is a ‘live’ reference to the actual, er, image source? So when I loop around and bring a new image into the ‘thumb’ image object I’m changing the referenced image for the existing imageSource chars too?

Damn. Well, I can see some ways of using this, but it wasn’t explained well in the dictionary. (Or I misunderstood the info, which is also very possible – I’ll go re-read the entry.)

Thanks!
Technical Writer, Meta
University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: imageSource problem

Post by Klaus » Tue Nov 06, 2018 10:33 am

Hi Keith,

what Jean-Marc said!

But do yourself a favour and do not use IT more than once!
IT will change when you least exspect IT! :D

Use IT only when neccessary and then put IT into another variable immediately!

Code: Select all

...
## No good:
## get fld "my list"
## Good:
put fld "mylist" into aVariable
repeat with x = 1 to the number of lines of aVariable
...
...
answer file "yadda yadda"
if IT <> empty then
   put it into tFile
end if
...
## etc...
Best

Klaus

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 346
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: imageSource problem

Post by thatkeith » Tue Nov 06, 2018 10:42 am

Thanks Klaus – very good advice. I’ve been caught by that in the past; I only leave it like this when I’m trying to isolate a behaviour by trimming things back. I shall go sanitise my code now. 8)
Technical Writer, Meta
University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist

Post Reply

Return to “Talking LiveCode”