Manish Agrawal
Manish Agrawal

Reputation: 11026

Save images for later use in iPhone

I want to save images that user selects from the camera roll, when app exits and restart i want to show those images to user. I have searched over internet and not found much useful resource. I know I can write the image in database as blob but I dont want to do it as increases the database size.
Is it possible to save the image path and then later use that path to access that image.

Upvotes: 0

Views: 2179

Answers (2)

Save the Image into the DocumentDirectory

NSString *imageName = "myImage.png";
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
NSString * documentsDirectoryPath = [paths objectAtIndex:0];

NSString *dataPath = [documentsDirectoryPath  stringByAppendingPathComponent:imageName];
NSData* settingsData = UIImagePNGRepresentation(imageData);

[settingsData writeToFile:dataPath atomically:YES];

EDIT:

If you want to see your saved images on the iPhone/iPad (or share them) in iTunes->Apps->Documents jut add in Info.plist "Application supports iTunes file sharing" : "YES"

Upvotes: 4

Shubhank
Shubhank

Reputation: 21805

You have to use UIImagePickerController to get the media from the Camera roll

You will get the image in the delegate method

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

Here you can get the image and save is as NSData .. in a file and then retrieve back it next time

Edit :

to Save

    Data = UIImageJPEGRepresentation(theImage,1.0);

       NSString  *imagePath = [ContentFolder stringByAppendingPathComponent:[NSString stringWithFormat:@" %d.plist",Counter]];
        NSLog(@" iamge path %@",imagePath);


        NSMutableDictionary *objectDictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:data,@"data", nil];

        [objectDictionary writeToFile:imagePath atomically:YES];

you should be able to change these according to your needs..

Upvotes: 1

Related Questions