Question

I have create an iPhone application. I have a button in a view. In the button click i want to get the photo from iPhone photo library. And set this selected photo in the button. How can i do that. thanks in advance.

Was it helpful?

Solution

OTHER TIPS

you have to use UIImagepicker .

A few days ago I do something like this for ipad.

Let call your button photoButton and it's a variable in our controller. Also add popoverController variable (it's needed to appropriate dismiss of popover).

When photoButton pressed we call next method:

- (void)photoButtonPressed:(id)sender {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = NO;
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
        popoverController.delegate = self;
        [popoverController presentPopoverFromRect:sender
                                           inView:self.view
                         permittedArrowDirections:UIPopoverArrowDirectionAny
                                         animated:YES];
        [imagePickerController release];
}

Also you should implement next methods to support UIImagePickerControllerDelegate and UIPopoverControllerDelegate protocol:

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)selectedImage
                  editingInfo:(NSDictionary *)editingInfo {
    [photoButton setImage:selectedImage forState:UIControlStateNormal];
    if ([popoverController isPopoverVisible]) {
        [popoverController dismissPopoverAnimated:YES];
    }
}

#pragma mark - UIPopoverControllerDelegate
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverContr {
    [popoverContr release];
    if (popoverContr == popoverController)
        popoverController = nil;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top