在 iPhone (OS 7) 上运行相机

Posted

技术标签:

【中文标题】在 iPhone (OS 7) 上运行相机【英文标题】:Running camera on iPhone (OS 7) 【发布时间】:2014-06-11 12:36:37 【问题描述】:

我的代码可以在 iPad 上完美运行

self.clockInEmployee = nil;
self.clockInEmployee = [[userInfo userInfo] valueForKey:@"employee"];
self.clockInEmployeeRole = [[userInfo userInfo] valueForKey:@"role"];
CLog (@"PIN %@", self.clockInEmployee.pin);

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = (id)self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = YES;
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picker.cameraOverlayView = self.cameraOverlay.view;
if (isPhone())

    picker.view.frame = CGRectMake(0, 748, 1024, 748);
    [mainNavController.view addSubview:picker.view];
    [UIView animateWithDuration:0.5 animations:^
        picker.view.frame = CGRectMake(0, 0, 1024, 748);

     completion:^(BOOL finished)
        [picker viewDidAppear:YES];
    ];

modViewController = picker;

但在 iPhone 上尝试启动相机时,我只观察到黑屏。

但如果我尝试这样启动它:

[appPresentingViewController presentViewController:picker animated:YES completion:nil];

成功启动。但在我的特定项目中,它会导致一些 UI 问题,因此无法使用。

我怎样才能做到这一点

if (isPhone())

    picker.view.frame = CGRectMake(0, 748, 1024, 748);
    [mainNavController.view addSubview:picker.view];
    [UIView animateWithDuration:0.5 animations:^
        picker.view.frame = CGRectMake(0, 0, 1024, 748);

     completion:^(BOOL finished)
        [picker viewDidAppear:YES];
    ];

适用于 iPad 和 iPhone 吗?

【问题讨论】:

【参考方案1】:

请试试这个:-

//create an overlay view instance
OverlayView *overlay = [[OverlayView alloc]
        initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];

//create a new image picker instance
UIImagePickerController *picker =
                [[UIImagePickerController alloc] init];
//set source to video!
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//hide all controls
picker.showsCameraControls = NO;
picker.navigationBarHidden = YES;
picker.toolbarHidden = YES;
//make the video preview full size
picker.wantsFullScreenLayout = YES;
picker.cameraViewTransform =
CGAffineTransformScale(picker.cameraViewTransform,
            CAMERA_TRANSFORM_X,
            CAMERA_TRANSFORM_Y);
//set our custom overlay view
picker.cameraOverlayView = overlay;

//show picker
[self presentModalViewController:picker animated:YES];

供进一步参考-here

【讨论】:

【参考方案2】:

你可以浏览一下 AVCAM 的 apples sample project 可以找到here

你需要做的是这样的:

AVCaptureSession *avcam = [[AVCaptureSession alloc] init];
    AVCaptureVideoPreviewLayer *previewLayer = 
        [AVCaptureVideoPreviewLayer layerWithSession:avcam];
    previewLayer.frame = self.previewView.bounds; 
   [self.previewView.layer addSublayer:previewLayer];

【讨论】:

以上是关于在 iPhone (OS 7) 上运行相机的主要内容,如果未能解决你的问题,请参考以下文章

在 iPhone 的 iOS 7 中打开相机时横向模式下的方向问题

UIImagePickerController 问题 - iPhone

保存用相机拍摄或从相机胶卷中选择的图像时出现长时间延迟 - iPhone

运行 iOS 8.2 的 ipad 中未显示相机

phonegap 相机适用于 iPhone,但不适用于 iPad

强制通用应用在 iPad 上运行 iPhone 版本