Assam Alzookery Assam Alzookery - 4 months ago 18
iOS Question

Simple Camera App Using UIImagePickerController

I created UIImageView to show the selected photo, and two buttons: one to take a new photo, and the other one to select a photo from the photo library. As showing in the image below.

Click here

When i select photo library, and i choose one of the images, it will appear on the UIImageView, But once i close the application and I reopen it again i don't see the image i chose anymore.

So how can i save the image on the application ?

ViewController.m

//-------------------------------------------------------------------------------------------------------------------------------------------------
- (IBAction)AddDriver:(id)sender {


UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Driver Names"
message:@"Please Enter your Full Name "
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// optionally configure the text field
textField.keyboardType = UIKeyboardTypeAlphabet;
}];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Add Driver"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
UITextField *textField = [alert.textFields firstObject];
textField.placeholder = @"Driver Name";

_drivername.text = textField.text;
}];
UIAlertAction* TakePhoto = [UIAlertAction
actionWithTitle:@"Take photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
picker.allowsEditing = false ;
[self presentViewController:picker animated:true completion:nil];
}

}];
UIAlertAction* OpenPhotoLibrary = [UIAlertAction
actionWithTitle:@"Open Photo Library "
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
picker.allowsEditing = true;
[self presentViewController:picker animated:true completion:nil];


}

}];
UIAlertAction* Save = [UIAlertAction
actionWithTitle:@"Save Photot"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSData *ImageData = UIImageJPEGRepresentation(_imagePicked.image, 0.6);
UIImage *compressedJPGImage = [UIImage imageWithData:ImageData];
UIImageWriteToSavedPhotosAlbum(compressedJPGImage, nil, nil, nil);
UIAlertView * alertt = [[UIAlertView alloc]initWithTitle:@"WOW" message:@"Your Image has been saved to photo library" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

[alertt show];
}];

UIAlertAction* noButton = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(@"You pressed button two");

}];

[alert addAction:okAction];
[alert addAction:TakePhoto];
[alert addAction:OpenPhotoLibrary];
[alert addAction:noButton];
[alert addAction:Save];

[self presentViewController:alert animated:YES completion:nil];

}

////-------------------------------------------------------------------------------------------------------------------------------------------------

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

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imagePicked.image = chosenImage;

[picker dismissViewControllerAnimated:YES completion:NULL];

}
////-------------------------------------------------------------------------------------------------------------------------------------------------

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:NULL];

}
//------------------------------------------------------------------------

Answer

You can use UserDefaults for that, First store image data in UserDefaults inside didFinishPickingMediaWithInfo method of UIImagePickerController

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imagePicked.image = chosenImage;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:UIImagePNGRepresentation(chosenImage) forKey:@"selectedImage"];
    [defaults synchronize];
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

After that get this image from UserDefaults in viewDidLoad of your ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults valueForKey:@"selectedImage"]) {
         self.imagePicked.image = [UIImage imageWithData:[defaults valueForKey:@"selectedImage"]];
    }
}