0

I want to save my picture that I took in the app. I want to save the picture so that even if I close the app the picture is still saved. this is my code that i have written until now

-(IBAction)TakePhoto {
    picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    [picker setSourceType:UIImagePickerControllerSourceTypeCamera];
    [self presentViewController:picker animated:YES completion:nil];
}

- (IBAction)ChooseExisting {
    picker2 = [[UIImagePickerController alloc]init];
    picker2.delegate = self;
    [picker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:picker2 animated:YES completion:nil];
}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [imageview setImage:image];
    [self dismissViewControllerAnimated:YES completion:nil];

}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:NULL];
}

How should I make the app save the picture that I took and display it.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Devnews
  • 1
  • 3
  • You want to save it in the user's camera roll? Or inside your own app? The former is just `UIImageWriteToSavedPhotosAlbum` C function. – Can Jul 16 '13 at 03:30

3 Answers3

1
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

UIImage * imageToSave = [UIImage imageNamed:@"Icon.png"];
NSData * binaryImageData = UIImagePNGRepresentation(imageToSave);

[binaryImageData writeToFile:[basePath stringByAppendingPathComponent:@"myfile.png"] atomically:YES];

Copied from here

You may need to edit the line with the imageToSave variable and change it to the UIImage variable, you got from the camera.

Community
  • 1
  • 1
Niklas
  • 23,674
  • 33
  • 131
  • 170
1

Try this In your method:

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

    //Save Your Image ======
    UIImage *yourImage  = imageView.image;//imageView.image;
    NSString *docDirPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docDirPath stringByAppendingPathComponent:@"myImageFile.png"];
    [UIImagePNGRepresentation(yourImage) writeToFile:filePath atomically:YES];



    [self dismissViewControllerAnimated:YES completion:nil];

}

And in your viewDidLoad of the same class write this:

 //For Get your Image from Documents Dir
    NSString *docDirPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docDirPath stringByAppendingPathComponent:@"myImageFile.png"];

    if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
//if file already saved then set it on your imageView
        UIImage *getYourImage = [UIImage imageWithContentsOfFile:filePath];
        imageView.image = getYourImage;
    }
    else
    {
//otherwise set a Dummy Image on your ImageView or nil
        imageView.image = nil; //[UIImage imageNamed:@"dummyImage.png"];

    }
Satish Azad
  • 2,302
  • 1
  • 16
  • 35
1

// Call this method to capture picture from camera

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    //Get image
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //Display in ImageView object (if you want to display it
    [imageView setImage:image];
        [ButtonName setImage:image forState:UIControlStateNormal];
    //Take image picker off the screen (required)
    [self dismissModalViewControllerAnimated:YES];
}

// to save an image :

- (NSString *)saveImage:(UIImage*)image:(NSString*)imageName 
{    
    NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.    
    NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
    NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
    NSLog(@"FULL PATH %@",fullPath);
    NSLog(@"image saved");
    return fullPath;
}

// to remove an image

- (void)removeImage:(NSString*)fileName 
{   

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", fileName]];

    [fileManager removeItemAtPath: fullPath error:NULL];

    NSLog(@"image removed");    
}

// to load an image from gallery

- (UIImage*)loadImage:(NSString*)imageName
 {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", imageName]];

    NSLog(@"Loading Image Path : %@",fullPath);

    return [UIImage imageWithContentsOfFile:fullPath];

}
Jitendra
  • 5,055
  • 2
  • 22
  • 42
SRK
  • 744
  • 3
  • 11
  • 23