iPhone Xcode相机集成教程

Leg*_*las 13 iphone camera cocoa-touch objective-c

我需要一些帮助.我需要将相机集成到我的应用程序中,我想了解以下内容:

  1. 我需要我的视图上的相机按钮,以便单击它打开相机视图.
  2. 我拍了一张照片
  3. 我需要编码,以便我可以访问电话库,然后在另一个视图中显示图片.

有谁能请我指出正确的方向?

Dee*_*olu 52

嗯,UIImagePickerController是你需要的工具.它将完成该清单中的大部分内容.

对于按钮,您可以创建带图形的自定义按钮,或者如果您计划使用工具栏或导航栏来按住按钮,则可以使用UIBarButtonSystemItemCamera系统项创建条形按钮.这将为您提供框架图像.

在点击它时,您将创建一个UIImagePickerController实例并以模态方式呈现它.

UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
[picker release];
Run Code Online (Sandbox Code Playgroud)

正如您必须注意到它具有delegate定义的属性,id < UIImagePickerControllerDelegate, UINavigationControllerDelegate> delegate;因此您必须采用这两种协议,但在大多数情况下,您只实现了两种方法 - imagePickerControllerDidCancel:imagePickerController:didFinishPickingMediaWithInfo:.UIImagePickerControllerDelegate协议中还有另一种方法,但已弃用.即使你在这里看到它提到了很多,也不要使用它.你会期望取消处理程序像这样写,

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissModalViewControllerAnimated:YES];
}
Run Code Online (Sandbox Code Playgroud)

其他方法是你做大部分事情的地方.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];

    // You have the image. You can use this to present the image in the next view like you require in `#3`.

    [self dismissModalViewControllerAnimated:YES];
}
Run Code Online (Sandbox Code Playgroud)

拍照是由UIImagePickerController实例自动完成的.但是,如果要覆盖其控件,可以通过设置然后实现自己的控件showsCameraControlsNO实现cameraOverlayView.如果您已经这样做并分配了一个按钮来拍摄照片,您实际上可以使用该takePicture方法触发照片操作.所以这应该解决#2.

您也可以使用其他属性来调整图像选择器.例如,您可以将用户限制为仅使用mediaTypes属性拍摄图像.