gpt4 book ai didi

ios - IOS自动抓图

转载 作者:行者123 更新时间:2023-11-28 20:12:45 25 4
gpt4 key购买 nike

我的要求是编写一个可以自动捕捉相机图片的示例 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com