Saving a photo as a file to be retrieved later

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

jekyllandhyde
Posts: 92
Joined: Thu Feb 14, 2013 7:17 pm

Saving a photo as a file to be retrieved later

Post by jekyllandhyde » Mon Apr 15, 2013 7:22 am

Well, after lots of trial and error I failed to figure out how to save a photo the user has just taken with the camera as a file. After it saves on my card I want to be able to display it again if the user quits the App and then returns later to the same screen/card. Every time the user takes a photo it will replace the previous photo.

Here is a portion of my button script, the question is what do I add at the end to save the jpg image as a file in the documents subdirectory of the iPhone and then what script do I need to retrieve it? Also where should the "retrieve file" script be place if not in preOpenCard? This is probably so simple, I'm a bit embarrassed to ask.

Code: Select all

--now we get a photo (button script)
   set the lockloc of the templateimage to true
   set the width of the templateimage to "240"
   set the height of the templateimage to "320"
   set the left of the templateimage to "205"
   set the top of the templateimage to "292"
   mobilepickphoto "rear camera", 960, 1280
   put the last image into tAttachment["data"]
   put "hazard.jpg" into tAttachment["name"]

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Saving a photo as a file to be retrieved later

Post by jmburnod » Mon Apr 15, 2013 8:54 am

Hi jekyllandhyde

To save an img to file in document folder:

Code: Select all

on ExportImgToFile pName
   put specialfolderPath("documents") & "/" & pName & "png" into tPathIMG
   put the short id of last img into tIdImg
   export image id tIdImg to tPathIMG as png
end ExportImgToFile
To retrieve it:
You have to write the tPathIMG in a file (preferences user).
On preopenstack you have to read this file and store its content somewhere (custom prop, global var)

Best regards
Jean-Marc
https://alternatic.ch

jekyllandhyde
Posts: 92
Joined: Thu Feb 14, 2013 7:17 pm

Re: Saving a photo as a file to be retrieved later

Post by jekyllandhyde » Tue Apr 16, 2013 6:07 am

I created an empty image box named "picture" on the page in exactly the same place where "the template image" is. I used your example to save the file then on preOpenCard I load it into the empty image box but nothing happens? Still confused. Perhaps my syntax is wrong?

Code: Select all

--in my photo taking button script
put specialfolderPath("documents") & "/hazardpicture" into tPathIMG  
put the short id of last img into tldImg
export image id tIdImg to tPathIMG as jpeg
---end of button script

on PreOpenCard
   -------this trys to display the last photo
put specialfolderpath("documents") & "/hazardpicture" into tPathgetIMG
   if there is not a file tPathgetIMG then
      put empty into URL ("file:" & tPathgetIMG)
   else
      put URL ("file:" & tPathgetIMG) into thelastImage
      put thelastImage into image "picture"
   end if
end PreOpenCard


jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Saving a photo as a file to be retrieved later

Post by jmburnod » Tue Apr 16, 2013 8:43 am

Hi kekylllandhide,

Use binfile to import data of the img OR "set the filename" to set the reference file of the img

Code: Select all

on PreOpenCard
      -------this trys to display the last photo
   put specialfolderpath("documents") & "/hazardpicture" into tPathgetIMG 
   if there is a file tPathgetIMG then
     -- put empty into URL ("file:" & tPathgetIMG) -- You make the file empty
      put URL ("binfile:" & tPathgetIMG) into last img -- import data into the image 
      --set the filename of last img to tPathgetIMG -- ref image file only
   else 
      put "" into last img -- imagedata = empty
      --set the filename of last img to empty -- ref image file = empty
   end if
end PreOpenCard
Best regards

Jean-Marc
https://alternatic.ch

jekyllandhyde
Posts: 92
Joined: Thu Feb 14, 2013 7:17 pm

Re: Saving a photo as a file to be retrieved later

Post by jekyllandhyde » Tue Apr 16, 2013 6:34 pm

Jean-Marc,

Thanks for your all your help. It seems that the script skips getting the file because it answers "there is no file". Very puzzling because I use a similar routine to save text into text files and it works great. I have used mobilepickphoto library because I am testing in the simulator. Here is my code.

Code: Select all

---button script to get a photo
   set the lockloc of the templateimage to true
   set the width of the templateimage to "240"
   set the height of the templateimage to "320"
   set the left of the templateimage to "205"
   set the top of the templateimage to "292"
   -- comment out the next line before finalizing code and swap with next line
   mobilepickphoto "library"
   --   mobilepickphoto "rear camera", 960, 1280
   put specialfolderPath("documents") & "/hazardpic" into tPathIMG
   put the short id of last img into tldImg
   export image id tldImg to tPathIMG as jpeg
----end button script

on PreOpenCard
put specialfolderpath("documents") & "/hazardpic" into tPathgetIMG
      if there is a file tPathgetIMG then
      put URL ("binfile:" & tPathgetIMG) into last img
      answer "there is a file"
      else 
            put "" into last img
      answer "there is no file"
      end if
   put last img into image "picture"
end PreOpenCard

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Saving a photo as a file to be retrieved later

Post by jmburnod » Tue Apr 16, 2013 6:49 pm

I think simulator is the problem, Try the same on the device. It should work
https://alternatic.ch

jekyllandhyde
Posts: 92
Joined: Thu Feb 14, 2013 7:17 pm

Re: Saving a photo as a file to be retrieved later

Post by jekyllandhyde » Tue Apr 16, 2013 8:35 pm

Just tested it but no luck.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Saving a photo as a file to be retrieved later

Post by jmburnod » Tue Apr 16, 2013 10:42 pm

I saw we forgot the .jpg suffix

Code: Select all

put specialfolderPath("documents") & "/hazardpic" & ".jpg" into tPathIMG
https://alternatic.ch

jekyllandhyde
Posts: 92
Joined: Thu Feb 14, 2013 7:17 pm

Re: Saving a photo as a file to be retrieved later

Post by jekyllandhyde » Wed Apr 17, 2013 4:45 am

I have a bunch of text files saved with no extension so I doubt it needs one. Having said that, on your advice, I changed the code and added the extension on the save and retrieve code. No difference, it still doesn't work.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Saving a photo as a file to be retrieved later

Post by Simon » Wed Apr 17, 2013 5:03 am

Not to be critical but saving an image file is not difficult.
Could you post a stack?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Saving a photo as a file to be retrieved later

Post by Dixie » Wed Apr 17, 2013 5:27 am

J&K...

instead of :

Code: Select all

put thelastImage into image "picture"
try:

Code: Select all

set the filename of image "picture" to tPathgetIMG
Dixie

jekyllandhyde
Posts: 92
Joined: Thu Feb 14, 2013 7:17 pm

Re: Saving a photo as a file to be retrieved later

Post by jekyllandhyde » Wed Apr 17, 2013 9:06 pm

I'm confused, if the code is answering "there is no image", how is "set the filename of image "picture" to tPathgetIMG" going to do anything? Clearly the code can't find a file? I'm a beginner so please correct me if I'm wrong?

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Saving a photo as a file to be retrieved later

Post by jmburnod » Wed Apr 17, 2013 10:12 pm

Hi jekyllandhyde,
I'm confused, if the code is answering "there is no image", how is "set the filename of image "picture" to tPathgetIMG" going to do anything?
Yes you're right
https://alternatic.ch

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Saving a photo as a file to be retrieved later

Post by Dixie » Wed Apr 17, 2013 10:19 pm

J&K...

I thought that you were past the problem of being able to save an image from the camera !... well, anyway... I have attached a stack that will let you take a photo... it will always display the last photo that was taken... and display the last photo that was taken when you relaunch the stack...

There are two handlers to look at... one in the 'camera' button and the other in the script of the stack...

Dixie
Attachments
gettingPhotos.livecode.zip
(6.66 KiB) Downloaded 310 times

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Saving a photo as a file to be retrieved later

Post by jmburnod » Wed Apr 17, 2013 10:28 pm

Hi Dixie,
I thought that you were past the problem of being able to save an image from the camera !
Yes. I already use a part of your script for long time and it work fine but I want understand what happen with the jekyllandhyde 's stack
Be well
Jean-Marc
https://alternatic.ch

Post Reply