ios 如何访问相机和相机胶卷 Objective-C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20991749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:01:54  来源:igfitidea点击:

How To Access Camera & Camera Roll Objective-C

iosiphoneobjective-cipadipod

提问by user3053939

I am making a Universal iOS Application which needs to access the camera and the camera roll. How would i go about this? I have no code to show yet because the app is mainly based around this.

我正在制作一个需要访问相机和相机胶卷的通用 iOS 应用程序。我会怎么做呢?我还没有要显示的代码,因为该应用程序主要基于此。

回答by gran33

This answer relevant on physical device ONLY!

此答案仅适用于物理设备!

Access Camera:

访问摄像头:

- (void)takePhoto {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];

}

Access Camera Roll:

访问相机胶卷:

- (void)selectPhoto {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];


}

Implementing the Delegate Methods of UIImagePickerController:

实现 UIImagePickerController 的委托方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

And This:

和这个:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

Source code here

源代码在这里

回答by Harun - Systematix

You need to declare delegate UIImagePickerControllerDelegate in .h file

您需要在 .h 文件中声明委托 UIImagePickerControllerDelegate

and use this code to open camera.

并使用此代码打开相机。

// Pick image from camera

- (IBAction)captureImage:(id)sender
{   
    if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIAlertView *deviceNotFoundAlert = [[UIAlertView alloc] initWithTitle:@"No Device" message:@"Camera is not available"
                                                                 delegate:nil
                                                        cancelButtonTitle:@"Okay"
                                                            otherButtonTitles:nil];
        [deviceNotFoundAlert show];

    } else {

        UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
        cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        cameraPicker.delegate =self;
        // Show image picker
        [self presentViewController:cameraPicker animated:YES completion:nil];
    }
}