Game Development Community

Image from URL, Save in App's Documents folder and Display Via Torque Script?

by John Sear · in iTorque 2D · 01/27/2010 (11:48 am) · 22 replies

Is it possible to:

- Get an Image (.png) from a URL - This image will be used in the games Store for In App Purchasing (DONE as a UIImage)

- Save this Image into the App's Documents Folder - Will need to use it at a later date if the user purchases a particular Pack without downloading again.(DONE)

- Use this image in Torque Script? - This is what I'm confused about.

The only way I can think of using the image that i get from the URL is by putting it into a new view in C Code and Displaying that view where I want it.

Is there a way of choosing a different folder other than one in the Resources when creating a ImageMapDatablock?

Thanks.
Page «Previous 1 2
#1
09/06/2010 (1:49 am)
@John -

I've run into this exact same issue. Have you (or has anyone else for that matter) come across a solution for this?

I've tried directly using the path obtained through the typical NSSearchPathForDirectories... spiel, but that did not work.

Thank you!
#2
09/06/2010 (3:20 am)
The first two parts are easy enough…but currently iT2D has a hardcoded path reference to the Documents folder. This won't work well at all on all systems, I'm sure. Easy enough to fix, if you know how to do it the right way on iOS :)

To actually use the image in any way you need a datablock for it, I guess. So just create a new object on the fly and add it to wherever you need it in the scene, after setting position and any other variables.

A "new t2dImageMapDatablock(name)" with just enough parameters should do the trick. Try that :)
#3
09/06/2010 (3:42 am)
@Ronny -

Thank you for your response. I should have clarified that it is, indeed, the third part where I am stuck.

The t2dImageMapDatablock is not the issue--I've been creating them dynamically all along in my app with images stored in "~/data/images/image_name.png".

The issue is with getting a usable path to the app's Documents directory, where I have images saved that are selected from the device's photo library.

I've had debug code spit out the path to a test saved image as "/var/mobile/Applications/Unique-ID-Hex/Documents/TestImage.png". However, using this just results in the classic "t2dImageMapDatablock Datablock is invalid!" error.

Regarding this:

Quote:
...iT2D has a hardcoded path reference to the Documents folder. This won't work well at all on all systems, I'm sure. Easy enough to fix, if you know how to do it the right way on iOS :)

...is there something I need to modify somewhere (duh) to make the path usable/recognizable by iT2D?

Could you be referring to this in iPhonePlatform.mm?

const char* Platform::getUserDataDirectory() 
{
   // application support directory is most in line with the current usages of this function.
   // this may change with later usage
   // perhaps the user data directory should be pref-controlled?
   //NSString *nsDataDir = [@"~/Library/Application Support/" stringByStandardizingPath];
	//-Mat using Documents directory in same folder as .app 
	NSString *nsDataDir = [@"../Documents" stringByStandardizingPath];
   return StringTable->insert([nsDataDir UTF8String]);
}

Any thoughts/directions?

Thank you
#4
09/06/2010 (5:13 am)
Yes, that's the function I don't like. Here's the right way (at least it shouldn't allocate anything you need to watch):
const char* Platform::getUserDataDirectory()
{
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  return StringTable->insert([[paths objectAtIndex:0] UTF8String]);
}

But that's really not important, as your debug output shows something that looks like a correct path.

Try specifying path names to files stored in Documents as if they are in the root of the Resources ("~/image.png" or "~/image" etc.). These special Torque search paths are supposed to look in both the application bundle and the Documents directory (whatever it's called if using the fix above - it might be called something different on devices set to a different locale - at least that's the case with desktop OS X).
#5
09/06/2010 (5:50 am)
My debug shows the correct path because I wrote my own function that is basically exactly what you posted (I've been working at this for the whole day at it's now almost 2am--I'm just burned out).

I tried accessing the root per your suggestion (~/my_image.png) but still get the same result error (or rather lack of functionality since the app doesn't crash--it just doesn't load the image).

"t2dImageMapDatablock Datablock is invalid!"

I've tried with several images landscape/portrait on both my iPhone 4 and my iPhone 2g (I haven't been able to test the iPad because I cannot even get a nib to load for me to use the required PopOver code for me to even get an ImagePicker shown). The results are identical and consistent.

Any further thoughts?
#6
09/06/2010 (7:32 am)
From memory ... The path is correct in debug but does not work for shipping. We had the same problem ... I don't have e code to hand so can't be more helpful - sorry. Search for TORQUE_SHIPPING might show what I'm gibbering on about.
#7
09/06/2010 (7:34 am)
I was sure there were some posts on how to do this correctly, but haven't found any yet. It's starting to look like a little more hacking is required :/

I know from my and other people's hacking that iT2D does not take changing paths around very well. Might need to dig into the image loader and see what it uses to build the paths. It shouldn't be making assumptions, but perhaps it does and always adds a certain base path to any string entered.
#8
09/06/2010 (7:36 am)
@Scott: We might have a substantial bug report in the making here ;)
#9
09/06/2010 (8:31 am)
To continue with Scott's post...

In resManager.cc, around line 1045 to line 1066, modify to ignore TORQUE_SHIPPING flag might solve your problem. Be aware of the comment from Torque dev team.

// Potentially dangerous behavior to have in shipping version but *very* useful
// in a production environment
//#ifdef TORQUE_TOOLS
//#ifndef TORQUE_SHIPPING
      // If we couldn't find the file in the resource list (generated
      // by setting the modPaths) then try to load it directly
      if (Platform::isFile(fileName))
      {
         ret = createResource (path, file);
         dictionary.pushBehind (ret, ResourceObject::File);

         ret->flags = ResourceObject::File;
         ret->fileOffset = 0;

         S32 fileSize = Platform::getFileSize(fileName);
         ret->fileSize = fileSize;
         ret->compressedFileSize = fileSize;

         return ret;
      }
//#endif
//#endif

HTH.
#10
09/06/2010 (7:17 pm)
Hi all;

I've followed the additional steps provided. Still, no joy.

The image I save from the picker gets stored at:
Quote:
/var/mobile/Applications/E6A006B7-9EA9-4874-AFB2-A9938DB23564/Documents/SavedImage.png
Yet, iT2D, when attempting to retrieve the image, thinks the path is:
Quote:
188/SavedImage.png

START: Rant

I will continue digging but these constant issues with this engine are unnerving. To date, I've had to fix device detection, orientation detection(all six positions), the subsequent handling of the rotation, the XCode project setup and library weak links, the jittery interpolation of mounted objects on large (greater than 150x150) sprites, and...oh, I'm certain there was more "tweaking" along the way.

I swear, Instant Action's response (or lack thereof) makes me feel as if by coming here to ask questions about an engine that should work as advertised without needing major code modification, it's like I'm getting on the T3D forum and asking...
Quote:
Can ne1s plz tells me how to make a MMORPG. It should be simple. I have great idea just need easy code.

By the time I get this project finished, I'll be ready to change the requisite logo to read "Powered by a heavily-modified version of Torque."

END: Rant


Any further input is still greatly appreciated. Also, I'll be starting another thread to see if any of you have been able to launch a nib from iT2D on the iPad.
#11
09/06/2010 (8:08 pm)
Brian, a quick one on those paths - it looks like your identifier is not setup in xCode (under properties).

I think also it needs setting in common/commonConfig.xml.

That should then mean it saves to an app directory instead of those UDID paths. Might help.
#12
09/06/2010 (8:34 pm)
Hi Scott;

By identifier, are you referring to the "Code Signing Identity"? If so, mine is set to "iPhone Developer: My_Name (XXXXXXXXXX)in 'Team Provisioning Profile: *'"

Per the modifications I made to get orientation/device detection/resolution setting to work, I am not using the commonConfig.xml file at all. Everything is set in defaultPrefs.cs.

Are you developing in Debug, Release, or Shipping mode in XCode? Mine has always been set to Debug. I just switched it to Release to see what kind of differences there would be and...yikes! I now have 21 errors.

Thoughts?
#13
09/06/2010 (8:55 pm)
I always develop in shipping unless I need the console. We found we couldn't save to the documents directory in shipping but we could in debug - my colleague An who posted earlier fixed this.

What do you have for properties -> identifier when you click on the target and do a Get Info?
#14
09/06/2010 (9:44 pm)
Well, with the intensive algorithms I had running, I needed to see a lot of feedback during testing, so I was always running in Debug.

However, I just switched to Shipping to see if there would be any difference in performance (and file retrieval results for that matter).

Now, I'm getting a "Failed to upload UA_Test.app" error, even though it sometimes actually seems to install (though I get no feedback in the Debugger Console).

My Identifier is set to: UB8EW7XFXY.com.visman-interactive.UA_Test
#15
09/06/2010 (10:13 pm)
Brian, let me check with An in the morning as it was him who discovered and fixed our original problem.
#16
09/06/2010 (10:18 pm)
Thanks Scott. I appreciate your assistance.

Does my Identifier look correct? Also, I did a clean build and no longer get the "Failed to upload..." error. However, nothing at all has changed with the path to Documents--that is, it is still being listed with the UDID in the path.

Once I can get past this hurdle and the iPad nib issue (which I'm certain will be an easier fix), I'll be set to do the GUI/menus for my app.

I completely saw the light at the end of the tunnel when this weekend started. Then, a train hit me from behind.
#17
09/07/2010 (3:36 am)
*Flushed*

hellosunshine.org/wp-content/uploads/2007/10/footinmouth.jpg
Well, I suppose it would help if the ConsoleFunction I wrote actually returned a "const char*" data type versus the default "bool" value from whichever method I had copied to get me going.

Now, if only I can find some embarrassing oversight on my non-loading iPad nib, I should be good to go. :)

For those whose time I have wasted seeking a solution, I apologize.
#18
09/07/2010 (6:49 am)
It's a relief having it working, and that's all that matters :)
#19
09/07/2010 (8:31 am)
The structure of an iPhone app deployed on device:
/<unique-app-id>/
|-Documents
|-yourapp.app

Torque loads resources within the .app package by default. So in order to make it load resources out of the .app package, you must provide the absolute filepath.

Supposed there's a console function name myDocumentRootPath() returns your ../Documents/ directory.
Example: /Users/annguyen/Library/Application Support/iPhone Simulator/3.2/Applications/75954E12-FC77-47FA-ADBF-37DAF48D4809/Documents

If your SavedImage.png file is saved at: ../Documents/SavedImage.png then in Torque script you will need to access it like this:
myDocumentRootPath() @ "/" @ SavedImage.png

--------------
If it still doesn't work for you then...

Save your SavedImage.png file at ../Documents/<your-identifier-string-defined-in-commonConfig.xml>/<your-app-name-defined-in-commonConfig.xml>/

And access the file using above method:
myDocumentRootPath() @ "/<your-identifier-string-defined-in-commonConfig.xml>/<your-app-name-defined-in-commonConfig.xml>/" @ SavedImage.png

Cheers.
#20
09/07/2010 (3:15 pm)
Hi An;

Thank you for taking the time to post this, as I'm sure it will be a big help for others down the road.

As I stated above, it was a simple oversight on my return data type in the console function I created that was my problem.

Still, I think it's wise for anyone to modify any hard-coded paths used by iT2D to use the methods provided by Apple and to just extend those via a console method.

To OP, if the above solutions work for you, you can set the thread to [RESOLVED]. That way, anyone else who wants to achieve this functionality will know the answers are here.
Page «Previous 1 2