Allow user to change image to one from their gallery
NickName:CoderJoe Ask DateTime:2015-06-11T12:04:43

Allow user to change image to one from their gallery

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

Copyright Notice:Content Author:「CoderJoe」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30771377/allow-user-to-change-image-to-one-from-their-gallery

Answers
Fernando Reynoso 2015-06-11T05:22:42

What you need is to implement UIImagePickerController with UIImagePickerControllerSourceTypePhotoLibrary as a source type.\n\nThere´re tons of tutorials and questions/answers here in stackoverflow. \n\nCheck for example this thread. It has pretty good answers with reference links to a sample code from Apple and other interesting tutorials. \n\nEDIT #2:\nWell, 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.\n\nEg:\nThis to save the image,\n\nNSData * data = UIImagePNGRepresentation(yourUImage);\nNSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];\n[defaults setObject:data forKey:@\"userImage\"];\n[defaults synchronize];\n\n\nand to get the image\n\n- (void)viewDidLoad \n{\n [super viewDidLoad];\n\n NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];\n NSData *data = [defaults objectForKey:@\"userImage\"];\n UIImage *image = [UIImage imageWithData:data];\n\n yourImageView.image = image;\n}\n",


Bhadresh Mulsaniya 2015-06-11T05:44:28

UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageAction:)];\n [self.imgPhotoStrand setUserInteractionEnabled:YES];\n [self.imgPhotoStrand addGestureRecognizer:tapGesture];\n\n -(void)imageAction:(UITapGestureRecognizer *)sender{ \n UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@\"Edit Photo\"\n delegate:self\n cancelButtonTitle:@\"Cancel\"\n destructiveButtonTitle:nil\n otherButtonTitles:@\"Camera\", @\"Select from Library\", nil];\n actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;\n [actionSheet showInView:self.view];\n }\n\n #pragma mark - UIActionSheetDelegate -\n\n- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex\n {\n switch(buttonIndex)\n {\n case 0:\n {\n if ([UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]) {\n\n if (IS_IPAD) {\n [[NSOperationQueue mainQueue] addOperationWithBlock:^{\n [self camera];\n }];\n }else if(IS_IPHONE){\n [self camera];\n }\n }\n else\n {\n UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:ALERTTITLE message:@\"No Camara Found\" delegate:nil cancelButtonTitle:@\"OK\" otherButtonTitles:nil, nil];\n [alertView show];\n }\n\n }\n break;\n case 1:\n {\n if (IS_IPAD) {\n [[NSOperationQueue mainQueue] addOperationWithBlock:^{\n [self library];\n }];\n }else if(IS_IPHONE){\n [self library];\n }\n\n }\n default:\n // Do Nothing.........\n break;\n }\n }\n\n\n - (void)camera {\n if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){\n return;\n }\n UIImagePickerController *picker = [[UIImagePickerController alloc] init];\n picker.sourceType = UIImagePickerControllerSourceTypeCamera;\n //Permetto la modifica delle foto\n picker.allowsEditing = YES;\n //Imposto il delegato\n [picker setDelegate:self];\n\n [self presentViewController:picker animated:YES completion:nil];\n }\n- (void)library {\n //Inizializzo la classe per la gestione della libreria immagine\n UIImagePickerController *picker = [[UIImagePickerController alloc] init];\n picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;\n //Permetto la modifica delle foto\n picker.allowsEditing = YES;\n //Imposto il delegato\n [picker setDelegate:self];\n\n [self presentViewController:picker animated:YES completion:nil];\n }\n\n\n #pragma mark UIImagePickerController Delegate\n - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{\n\n [self dismissViewControllerAnimated:YES completion:^{\n\n\n\n UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];\n self.imgPhotoStrand.image =image;\n\n }];\n\n\n }\n - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{\n [self dismissViewControllerAnimated:YES completion:nil];\n }\n",


More about “Allow user to change image to one from their gallery” related questions

Allow user to select camera or gallery for image

What I'm trying to do seems very simple, but after a few days of searching I can't quite figure it out. I have an application that allows the user to select multiple(up to 5) images. I'm using an

Show Detail

Allow user to change image to one from their gallery

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 ha...

Show Detail

Change image in gallery

I have a gallery where I'm displaying a number of ID cards as images. When the user clicks on an image, I want to change the image to another one which shows the 'back' of the card. I have bound an

Show Detail

Single intent to let user take picture OR pick image from gallery in Android

I'm developing an app for Android 2.1 upwards. I want to enable my users to select a profile picture within my app (I'm not using the contacts framework). The ideal solution would be to fire an ...

Show Detail

Allow user to choose an image from their gallery to upload to imageview in an activity?

I am not sure what to search for, I have tried a few things but have not found what I need. Basically I have a default image for my app that is the logo displaying in an imageview. I have and "admin"

Show Detail

Cropping Image by user selecting image from gallery

I am creating an application in android in which I want to let the user select his profile pic from a photo gallery. I want to let the user crop an image for his profile picture when selecting the ...

Show Detail

Change size of image picked from image gallery

I want to change the size of an image selected from the photo gallery. Is it possible? I'm getting an image sized (320, 290) and I want to change the image size to (480, 320).

Show Detail

change position of image in image gallery using php

Hi i am working on a photo gallery. Here all images are coming from mysql database table. The images are uploaded from the admin panel. I am using php to upload and fetch images from database table...

Show Detail

jQuery Image Change - Gallery View

I have a large image in a gallery that needs to change when one of the thumbnails is clicked within a unordered list below. The images are coming in dynamically so the jquery script needs to be abl...

Show Detail

Image is picked from gallery but not viewed

I am developing a quiz app in which I have a profile page were the user can change their image when they needed. It was working properly, the profile page of the user is a fragment not an activity. I

Show Detail