Reputation: 3
I was working with an xcode project(game with cocos2d) that uses a lot of sprites. The thing is I would want to download images from the URL to a local directory on the device, so that i can use them later to create sprites.
So what would be the best location to download the images to? I can create a folder reference inside my projects folder, but is that a good way to go about it? Is there a standard practice to this?
I read that there are iphone which can be used for this? Please let me know if there are resources that can help me understand these better....
Thanx
Upvotes: 0
Views: 826
Reputation: 1
try saving in NSDocumentsDirectory or the Library Directory and u can get it from the plist
Upvotes: 0
Reputation: 7844
You can download all images form the URl to DocumentDirectory and you can use it later, as a part of your game. I am using the following code to download Images
NSArray* path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString* docPath = [path objectAtIndex:0];
docPath = [docPath stringByAppendingPathComponent:@"ImageName.png"];
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[array objectAtIndex:o] valueForKey:@"Image_Question"]]]];
NSData* imgData = UIImagePNGRepresentation(image);
[imgData writeToFile:docPath atomically:YES];
And when you want to retrieve for any usage in application you can use the following code
UIImage* imageFormDoc = [UIImage imageWithContentsOfFile:docPath];
and when you want to delete the image form the Document Directory you can use the following code.
[[NSFileManager defaultManager] removeItemAtPath:docPath error: &error];
docPath is the full path of Document Directory with Image then you can Print Using NSLog for detail information.emphasized text
Upvotes: 1
Reputation: 475
Yes , You can save all the images to the Document folder of the Application and later on , you can use all the images in your application.
Upvotes: 0