Saving Images

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Saving Images

Postby Lenko » Tue Jan 17, 2017 2:30 pm

I am trying to save images so I don't overwrite the previous save.I am saving an image count and adding the variable nPic.
The example shown will save the image OK. You can see the line that doesn't work but the file name works correctly.
on mouseUp
---image count
set the defaultFolder to specialFolderPath("desktop")
put URL ("file:Image_Count.txt")into field "Image Count"
put the text of field "Image Count" into nPic
add 1 to nPic
wait 4 ticks
put nPic into field "Image Count"
put field "Image Count" into URL ("file:Image_Count.txt")

-- the next line saves image OK
put specialfolderpath ("desktop") & "/Image.png" into tFile
-- the next line saves the file name correctly but saves the file with o bytes /no image###
###put specialfolderpath ("desktop") & "/Image.png" && nPic into tFile

## This is only necessary, if you are on a Mac!
set the filetype to ""

---set the selected of image "Display" to true----not sure if I need this line??
--- export image to file: either line works
export img "Display" to file tFile as png
--export snapshot from img "Display" to file tFile as png

end mouseUp
This is all that is preventing me from complete my app.
Any suggestions greatly appreciated
Posts: 8
Joined: Fri Jan 16, 2015 2:50 pm

Re: Saving Images

Postby SparkOut » Tue Jan 17, 2017 4:27 pm

You are not correctly concatenating the string to put into tFile variable. Try:

put specialFolderPath ("desktop ") & "/image_" & nPic & ".png" into tFile

Hope that helps
Posts: 1548
Joined: Sun Sep 23, 2007 4:58 pm

Re: Saving Images

Postby FourthWorld » Tue Jan 17, 2017 6:18 pm

Half of good programming is error-checking. In LiveCode, when a command fails checking "the result" immediately after that command will allow you to know if an error is being reported to you. The sysError function goes further, returning the OS error code for the specific error condition.

Adding an error check after the export command would reveal the path issue:
Code: Select all
export img "Display"  to file tFile  as png
if the result is not empty then
  answer "Couldn't save image: "& the result &"("& sysError() &")"
  exit to top
end if
Richard Gaskin
Community volunteer LiveCode Community Liaison

and developer at Fourth World Systems:
LiveCode User Group on Facebook :
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4945
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles

Re: Saving Images

Postby Lenko » Tue Jan 17, 2017 9:46 pm

I tried the line as suggested
put specialFolderPath ("desktop ") & "/image_" & nPic & ".png" into tFile

on the export line I get an error
(export: can't open file (or mask file))

This is a new one on me,

I am still at a loss, I appreciate your fast response.
Posts: 8
Joined: Fri Jan 16, 2015 2:50 pm

Re: Saving Images

Postby SparkOut » Tue Jan 17, 2017 10:21 pm

can you add a couple of lines:

answer nPic


answer tFile

before the export attempt, so you can be sure what the variables contain. If (say) nPic does not contain a single value because it has been populated from the field "Image Count" which has got a hidden cr in it, then this would cause problems.

Another wild thought - is this for mobile (iOS or Android)? specialFolderPath("desktop") would not apply, although you should get a different response, I believe. On second (wild) thoughts, that can't be, as you are getting a saved file to the desktop when you leave out the nPic count. So I would definitely look closely at what nPic contains and what the concatenated tFile path looks like.
Posts: 1548
Joined: Sun Sep 23, 2007 4:58 pm

Re: Saving Images

Postby Lenko » Wed Jan 18, 2017 1:31 am

I went to the property inspector and cleared the text to be sure there was no cr
and unchecked shared text and lock text.
I did use answer during my debugging .
contains number from image count routine which is working fine.

Now the app will keep track of the saved images with a number and will not overwrite the previous save.

The app will be for mobile. I have my keystore and ios certificates and have had successful builds
on both of my devices. I will make the changes for my mobile versions and will be submitting them soon.
I greatly appreciate your fast response in helping me.
Many Many Thanks
Posts: 8
Joined: Fri Jan 16, 2015 2:50 pm

Re: Saving Images

Postby Klaus » Wed Jan 18, 2017 6:07 pm

Hi Lenko,

on MOBILE make sure you save/store everything in: specialfolderpath("documents")
That is the only folder where we have definitively write permissions!



Since ths topic is really not "Creating Games" specific, I will move this thread to the "Beginners" section!
Posts: 8982
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany

Return to Getting Started with LiveCode - Complete Beginners

Who is online

Users browsing this forum: No registered users and 3 guests