我正在创建一个从iphone相机捕获图像并在创建的应用程序中显示它的应用程序我在ios开发人员库中阅读了一些文章,并在.m文件中编写了此代码
- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
// Displays a control that allows the user to choose picture or
// movie capture, if both are available:
cameraUI.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
[controller presentModalViewController: cameraUI animated: YES];
return YES;
}
还创建了一个与此代码链接的按钮, 它调用此函数但控件不会传递给上面的函数
- (IBAction) showCameraUI {
[self startCameraControllerFromViewController: self
usingDelegate: self];
}
我也不知道这段代码是否有用
有人有更好的主意。 谢谢, 阿伦。
答案 0 :(得分:2)
首先将委托分配给它的源文件
然后当您按下按钮时,此方法会在您的应用中打开相机控制器
首先为iPad创建PopOverController,因为我们无法直接打开ImagePicker控制器
编辑:添加了UIPopOverController
- (IBAction) showCameraUI {
BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = hasCamera ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;
if (self.popoverController != nil) {
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController=nil;
}
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
CGRect popoverRect = [self.view convertRect:[yourBtn frame]
fromView:[yourBtn superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 100) ;
popoverRect.origin.x = popoverRect.origin.x;
[self.popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
实现此委托方法以获取捕获的图像
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *yourImageView = image;
if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
self.popoverController=nil;
}
}
如果用户取消控制器,则实施此方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
[picker dismissModalViewControllerAnimated:YES];
if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
self.popoverController=nil;
}
}
- (void)pickerDone:(id)sender
{
if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
self.popoverController=nil;
}
}
答案 1 :(得分:0)
您可以使用委托:UIImagePickerControllerDelegate
请使用以下方式:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)mediaAndInfo
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker