iPhone Xcode 相机集成教程

发布于 2024-11-16 00:01:32 字数 1539 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2024-11-23 00:01:32

好吧, UIImagePickerController 是您需要的工具。它将完成该清单中的大部分内容。

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

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

UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
[picker release];

您一定已经注意到,它有一个 delegate 属性,定义为 id id delegate 。 UIImagePickerControllerDelegate、UINavigationControllerDelegate> delegate; 因此您必须采用这两种协议,但在大多数情况下您只实现两个方法 - imagePickerControllerDidCancel:imagePickerController:didFinishPickingMediaWithInfo:UIImagePickerControllerDelegate 协议中还有另一种方法,但已弃用。即使您看到这里多次提到它,也不要使用它。您可能希望取消处理程序像这样编写,

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissModalViewControllerAnimated:YES];
}

其他方法是您执行大部分操作的地方。

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

拍照是由 UIImagePickerController 实例自动完成的。但是,如果您想覆盖它们的控件,可以通过将 showsCameraControls 设置为 NO,然后实现您自己的 cameraOverlayView 来实现。如果您已这样做并分配了一个按钮来拍照,则实际上可以使用 takePicture 方法触发拍照操作。所以这应该解决#2

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

Well, UIImagePickerController is the tool you need. It will do most of the stuff in that checklist.

For the button you can create a custom button with graphics or if you are planning to use a tool bar or a navigation bar to hold your buttons, you can create the bar button using the UIBarButtonSystemItemCamera system item. This will give you the framework image.

On tapping it, you will create a UIImagePickerController instance and present it modally.

UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
[picker release];

As you must've noticed that it has a delegate property which is defined as id < UIImagePickerControllerDelegate, UINavigationControllerDelegate> delegate; so you will have to adopt both the protocols but in most cases you implement only two methods – imagePickerControllerDidCancel: and imagePickerController:didFinishPickingMediaWithInfo:. There is another method in UIImagePickerControllerDelegate protocol but that's deprecated. Don't use it even if you see it mentioned a lot around here. You would expect the cancel handler to be written like this,

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissModalViewControllerAnimated:YES];
}

The other methods is where you do most of the stuff.

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

Taking the picture is done automatically by the UIImagePickerController instance. However if you want to override their controls, you can do so by setting showsCameraControls to NO and then implementing your own cameraOverlayView. If you've done so and have assigned a button to take the picture, you can actually trigger the picture action using the takePicture method. So this should address #2.

You can use other properties to adjust your image picker too. For example, you can limit the user to only taking images using the mediaTypes property.

逆光飞翔i 2024-11-23 00:01:32

解释一下文档,从 iOS6 开始,dismissModalViewControllerAnimated: 已被弃用。请改用 dismissViewControllerAnimated:completion:

Paraphrasing the docs, dismissModalViewControllerAnimated: is deprecated from iOS6 onwards. Use dismissViewControllerAnimated:completion: instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文