我创建了一个iPhone应用程序。我在视图中有一个按钮。在按钮单击我想从iPhone照片库中获取照片。并在按钮中设置此选定的照片。 我怎样才能做到这一点。 提前谢谢。
答案 0 :(得分:2)
你必须使用UIImagepicker。
答案 1 :(得分:2)
您可以按照本教程从手机图库或相机中获取图像。 http://www.icodeblog.com/2009/07/28/getting-images-from-the-iphone-photo-library-or-camera-using-uiimagepickercontroller/
答案 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;
}