Allow user to change image to one from their gallery

114 views Asked by At

I have a UIImageView that has a UIImage. What i would like is when the UIImage is clicked the user can select a photo from thier gallery to show in the UIImageView instead of the default image i have set

    imageView = [[UIImageView alloc] init];
    UIImage *myimg = [UIImage imageNamed:@"ProfilePic.png"];
    imageView.image=myimg;
    imageView.frame = CGRectMake(100,0, 80, 50); // pass your frame   here
    [setsView addSubview:imageView];
    imageView.layer.backgroundColor=[[UIColor clearColor] CGColor];
    imageView.layer.cornerRadius=20;
    imageView.layer.borderWidth=2.0;
    imageView.layer.masksToBounds = YES;
    imageView.layer.borderColor=[[UIColor redColor] CGColor];

EDIT: If I could make a view with a bunch of images and when an image is clicked it changes the "myimg" to the clicked image if that makes sense

EDIT #2:Also how would u save the photo the user chose and use it the next time the user opened the appliation

3

There are 3 answers

10
Fernando Reynoso On BEST ANSWER

What you need is to implement UIImagePickerController with UIImagePickerControllerSourceTypePhotoLibrary as a source type.

ThereĀ“re tons of tutorials and questions/answers here in stackoverflow.

Check for example this thread. It has pretty good answers with reference links to a sample code from Apple and other interesting tutorials.

EDIT #2: Well, this also has many ways to be accomplished all depends on your use case. But a quick and simple way to do it is by using NSUserDefaults.

Eg: This to save the image,

NSData * data = UIImagePNGRepresentation(yourUImage);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"userImage"];
[defaults synchronize];

and to get the image

- (void)viewDidLoad 
{
    [super viewDidLoad];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [defaults objectForKey:@"userImage"];
    UIImage *image = [UIImage imageWithData:data];

    yourImageView.image = image;
}
0
Bhadresh Mulsaniya On
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageAction:)];
                    [self.imgPhotoStrand setUserInteractionEnabled:YES];
                    [self.imgPhotoStrand addGestureRecognizer:tapGesture];

 -(void)imageAction:(UITapGestureRecognizer *)sender{ 
   UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Edit Photo"
                                                                                 delegate:self
                                                                        cancelButtonTitle:@"Cancel"
                                                                   destructiveButtonTitle:nil
                                                                        otherButtonTitles:@"Camera", @"Select from Library", nil];
                    actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
                    [actionSheet showInView:self.view];
 }

 #pragma mark - UIActionSheetDelegate -

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
            {
                switch(buttonIndex)
                {
                    case 0:
                    {
                        if ([UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]) {

                            if (IS_IPAD) {
                                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                     [self camera];
                                }];
                            }else if(IS_IPHONE){
                                 [self camera];
                            }
                        }
                        else
                        {
                            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:ALERTTITLE message:@"No Camara Found" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                            [alertView show];
                        }

                    }
                        break;
                    case 1:
                    {
                        if (IS_IPAD) {
                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self library];
                            }];
                        }else if(IS_IPHONE){
                            [self library];
                        }

                    }
                    default:
                        // Do Nothing.........
                        break;
                }
            }


        - (void)camera {
            if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
                return;
            }
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            //Permetto la modifica delle foto
            picker.allowsEditing = YES;
            //Imposto il delegato
            [picker setDelegate:self];

            [self presentViewController:picker animated:YES completion:nil];
        }
- (void)library {
            //Inizializzo la classe per la gestione della libreria immagine
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            //Permetto la modifica delle foto
            picker.allowsEditing = YES;
            //Imposto il delegato
            [picker setDelegate:self];

            [self presentViewController:picker animated:YES completion:nil];
        }


 #pragma mark UIImagePickerController Delegate
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

        [self dismissViewControllerAnimated:YES completion:^{



                UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
                 self.imgPhotoStrand.image =image;

        }];


    }
 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
3
Pradumna Patil On

You can use UIImagePickerController to get image from gallery and show it on image view

This code takes you to Gallery

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];

And then here is delegate method for UIImagePickerController

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

    chosenImage = info[UIImagePickerControllerEditedImage];
    yourImageView.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

This worked for me. Hope it helps.

1]You can use custom button for each image so that when you click an image button target gets called and you can change the your image with the clicked image.

2]For storing the image you can use NSUserDefaults so that even though application gets closed your image is stored in NSUserDefaults So that you can use it the next time the user opened the application.

NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1.0);
encodedString = [imageData base64EncodedStringWithOptions:0];
NSLog(@"chosenImage : %@",chosenImage);

This will give you your image.

To set this image to ImageView you can use

UIImage *image = [UIImage imageWithData:data];
yourImageView.image = image;

Hope it helps.