Set front-facing camera in Xcode

前端 未结 3 1328
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 07:36

Set front-facing camera in Xcode. How can I do that? Set front-facing camera in Xcode.

AVCaptureSession *session = [AVCaptureSession new];
if ([[UIDevice cur         


        
相关标签:
3条回答
  • 2020-12-05 08:08

    Since iOS 10 devicesWithMediaType: is deprecated. You should use AVCAptureDeviceDiscoverySession.

    - (AVCaptureDevice *)frontCamera {    
        return [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[ AVCaptureDeviceTypeBuiltInWideAngleCamera ]
                                                                      mediaType:AVMediaTypeVideo
                                                                       position:AVCaptureDevicePositionFront].devices.firstObject;
    }
    
    0 讨论(0)
  • 2020-12-05 08:19

    Add this method to iterate over existing devices checking its position:

    - (AVCaptureDevice *)frontCamera {
        NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        for (AVCaptureDevice *device in devices) {
            if ([device position] == AVCaptureDevicePositionFront) {
                return device;
            }
        }
        return nil;
    }
    

    Then, replace:

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    

    with:

    AVCaptureDevice *device = [self frontCamera];
    
    0 讨论(0)
  • 2020-12-05 08:31
    **.h file
    @interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
    .m File
    -(IBAction)Btn_Ok:(id)sender
    {
     if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
                {
                    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                          message:@"Device has no camera"
                                                                         delegate:nil
                                                                cancelButtonTitle:@"OK"
                                                                otherButtonTitles: nil];
                    [myAlertView show];
                }
                else
                {
                    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                    picker.delegate = self;
                    picker.allowsEditing = YES;
                    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                    picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;
                    [self presentViewController:picker animated:YES completion:NULL];
                }
    }
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
        self.yourimg.image = chosenImage;
        [picker dismissViewControllerAnimated:YES completion:NULL];
    }
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    {
        [picker dismissViewControllerAnimated:YES completion:NULL];
    }
    
    0 讨论(0)
提交回复
热议问题