从iPhone照片库获取照片

时间:2012-03-07 08:07:19

标签: iphone photo photolibrary

我创建了一个iPhone应用程序。我在视图中有一个按钮。在按钮单击我想从iPhone照片库中获取照片。并在按钮中设置此选定的照片。 我怎样才能做到这一点。 提前谢谢。

3 个答案:

答案 0 :(得分:2)

你必须使用UIImagepicker

答案 1 :(得分:2)

答案 2 :(得分:1)

前几天我为ipad做了类似的事。

让我们调用您的按钮photoButton,它是我们控制器中的一个变量。还要添加popoverController变量(需要适当关闭popover)。

当按下photoButton时,我们调用下一个方法:

- (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];
}

此外,您应该实现下一个方法来支持UIImagePickerControllerDelegate和UIPopoverControllerDelegate协议:

#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;
}