作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的要求是编写一个可以自动捕捉相机图片的示例 IOS 应用程序。使用提供的各种 S.O 链接,我确实实现了以下代码 -
我的 CameraViewController.h 类定义如下:
@interface CameraViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *ImageView;
@end
CameraViewController.m 有以下代码:
-(void)viewDidAppear:(BOOL)animated
{
NSLog(@"Setting the background now");
UIImagePickerController *picker = [[UIImagePickerController alloc] init];picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
picker.showsCameraControls = NO;
picker.navigationBarHidden = NO;
picker.toolbarHidden = NO;
[self presentViewController:picker animated:YES completion:NULL];
NSLog(@"Taking the picture now");
[picker takePicture];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(@"Entered the case of finishing pictures");
}
- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker
{
NSLog(@"Entered the case of cancel");
}
上面的代码所做的是成功启动相机应用程序,但是我不确定 takePicture API 是否能够成功单击图片。我在 Ipad 的“照片”应用程序中看不到任何已保存的图片,因此我假设图片尚未被点击。有人可以告诉我上面的代码是否正确,或者我需要做什么才能在显示相机控件后自动执行单击捕获按钮的部分
最佳答案
[请转到 Apple 文档中的“使用 UIImagePickerController 来选择图片和拍摄照片”,了解类 UIImagePickerController
的属性 cameraOverlayView
,了解一个完整的示例应用程序,该应用程序执行以下操作您需要,以及更多。]
您将 CameraViewController
指定为采用 UIImagePickerControllerDelegate
协议(protocol),因此您必须实现两条消息:
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info;
和
- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker;
如 iOS 文档所述,NSDictionary* 信息
有一个键 UIImagePickerControllerOriginalImage
,它将返回 UIImage
。像这样访问它:
UIImage *snapshot = (UIImage *) [info objectForKey: UIImagePickerControllerOriginalImage];
由于您的计划是使用 takePicture
自动拍照(无需用户交互),因此请务必指定
picker.showsCameraControls = NO;
关于ios - IOS自动抓图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19505135/
我是一名优秀的程序员,十分优秀!