parisianastro97
parisianastro97

Reputation: 51

Set button image as selected image from library in xcode

I am making an app where I select a button, and it leads me to the Library to select a picture. I want that selected picture to become the main image of the button. Can anybody help me?

Here is the code that I used to reach into the library:

- (IBAction)image1Pressed:(id)sender {

if([UIImagePickerController isSourceTypeAvailable:
    UIImagePickerControllerSourceTypePhotoLibrary])
{
    UIImagePickerController *picker1= [[UIImagePickerController alloc]init];
    picker1.delegate = self;
    picker1.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker1 animated:YES completion:nil];
    [picker1 release];
}
}

Any help will be greatly appreciated!

Thanks

Upvotes: 0

Views: 1695

Answers (1)

Nils Hott
Nils Hott

Reputation: 1690

You need to create an outlet for the button, let's name it "imageButton". The let the image picker do the rest:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *btnImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    [imageButton setImage:btnImage forState:UIControlStateNormal];
}

Done.

Upvotes: 1

Related Questions