saveImage / readImage in xcode

Hello all,

I’m trying to download some images, save them as sprites, and then load them when needed.

I have a “photo” class to store the image, a filename, url and other attributes for it. Here is the snippet of the code I’m having trouble with:

        local theImage = readImage("Documents:" .. self.filename)
        
        if theImage == nil then 
            http.request(self.url, function(theImage, status, head)
                self.photo = theImage
                saveImage("Documents:" .. self.filename, theImage)
                print("saved " .. self.filename)
                end)
        else

            self.photo = theImage
        end

This code is part of a method called when I need the image. It should download it, unless the file is already in the “Documents” folder.

This works perfectly within Codea. My ‘Documents’ sprite folder gets populated with the images I download, and every time I run the project the images are already there. Great.

Now, when I export my project to xcode (using the latest export option in Codea), and try to run it on my ipad, readImage fails. That is, the first time the code runs my files are downloaded and saved, but after that, readImage doesn’t find them. I can see through the xcode Organizer window that the images are being downloaded and saved to Documents. I can also add a few lines using spriteList, and I get the list of all the images. But readImage keeps returning nil.

What am I missing? Is there another way to specify the path to stuff in ‘Documents’ so it works on xcode?

Thanks!

@juanbuhler Xcode export also makes a Documents.spritepack folder — you will find this in SpritePacks/Documents.spritepack in the exported Xcode folder structure. Remove this folder and your exported Codea project will use your iPad’s Documents/ folder instead (which is read/writable).

@simeon Perfect! It took a coupe of tries, I had to make sure the old version was deleted from the ipad, but it worked. Thanks!

BTW, this is what I’m working on–the video is a few days old so the app is looking a bit better than that. It’s a portfolio for my photographs:

http://www.youtube.com/watch?v=3QoxBfYqQLg

One of the galleries will be my photoblog. The idea is to explore what a photo book should look nowadays. I started months ago making a book for the ipad, but dropped that idea because it’s too constraining. No reason nowadays a photo book shouldn’t get updates automatically and also serve as a mean of communication between a photographer and the people who want to follow his/her work.

Now I would love it if the save/read image thing could do jpegs as well and not only png…

@juanbuhler Awe man are all those pictures yours? 'Cause those looks amazing, real art man! \m/

@akaJag

:"> Thanks! Yes, those are my photos. I work in computer graphics, but on the side I’ve been a photographer for the last 15 years. Here is my web site: http://www.jbuhler.com

Wow that is just… Wow
Speachless!
When I think of pictures like those I imagine they popup from nowhere like everything else that’s awesome. But no, there an amazing dude behind making that cool shit.
Speachless!

@juanbuhler amazing app! Are the layouts computed dynamically, or did you manually arrange the tiles?

readImage should be able to load .jpg files — I’ll double check. Saving does use png, though (because it maintains alpha).

@Simeon Thanks! I thought about computing the layouts dynamically. It would be a fun problem to tackle, and easy in my case since all my photos are 3:2 aspect ratio. But in the end I wanted the control. The galleries and their content are specified from a file served from my web site, and that file includes positions for each image on a grid five rectangles high by n. For my photoblog, which needs to automatically update, I’m having Wordpress serve a json file, and the layout is hard coded in the app, as a table of triplets: x position, y position and size.

Still have a bit to go before I make this public. Right now the file with the layout and photos is in an ad hoc text format. It should also be json. I’m also adding text describing galleries and such.

I think readImage does read jpegs. But the photos are not included in my app (that would be too much data!), they are downloaded the first time you go to each gallery and saved. So I’m stuck with png, because saveImage doesn’t have a format flag. I don’t think this is a huge deal though–if that functionality is added in the future I’ll use it, but its fine for now.