Lakan Bahande
Lakan Bahande

Reputation: 165

Objective-C: Save Image When the App is Killed

I am on a development of my school project and i chose to build an app that uses the camera. I have manage to use it and attach it into UIImageView, but i am having a hard time on making that image permanent when the app is killed.

I mean, i want the image to remain on the view where i attached it when i killed the app and open it again, because in my current project when i killed the app the image is also gone when i open the app again.

Here's my code:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    insertPhoto1.contentMode=UIViewContentModeCenter;
    [insertPhoto1 setImage:image];
    [self dismissModalViewControllerAnimated:YES];
}

And this for Tap:

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            {
                [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
            }
            else
            {
                [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            }
            [imagePicker setDelegate:self];
            [self presentModalViewController:imagePicker animated:YES];

Upvotes: 0

Views: 448

Answers (2)

Brendt
Brendt

Reputation: 397

You can save and retrieve the image from disk like so:

Saving the image:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
    NSString* imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/photo1.png"];
    [imageData writeToFile:imagePath atomically:YES];

    insertPhoto1.contentMode = UIViewContentModeCenter;
    [insertPhoto1 setImage:image];
    [self dismissModalViewControllerAnimated:YES];
}

Retrieving the image in the UIViewController viewWillAppear:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSString* imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/photo1.png"];
    UIImage* imageFromFileSystem = [UIImage imageWithContentsOfFile:imagePath];

    UIImageView imageView = [[UIImageView alloc] initWithImage:imageFromFileSystem];
    [self.view addSubView:imageView];
}

Upvotes: 1

Deepjyoti Roy
Deepjyoti Roy

Reputation: 482

One option is to save the photo in NSUserDefaults when the app is killed. Saving the image:

UIImage* image = [UIImage imageNamed:@"example_image.png"];
NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
NSString* imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/saved_example_image.png"];
[imageData writeToFile:imagePath atomically:YES];

Then you can call the image when your app is started again. Retrieving the image:

NSString* imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/saved_example_image.png"];
UIImage* imageFromFileSystem = [UIImage imageWithContentsOfFile:imagePath];

Upvotes: 0

Related Questions