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

我正在制作一个需要访问相机和相机胶卷的通用iOS应用程序。 我将如何去做这件事? 我没有代码显示,因为该应用程序主要是基于此。

这个答案只适用于物理设备!

访问摄像机:

 - (void)takePhoto { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:picker animated:YES completion:NULL]; } 

访问相机胶卷:

 - (void)selectPhoto { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:picker animated:YES completion:NULL]; } 

实现UIImagePickerController的委托方法:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; self.imageView.image = chosenImage; [picker dismissViewControllerAnimated:YES completion:NULL]; } 

和这个:

 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:NULL]; } 

源代码在这里

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

并使用此代码打开相机。

 // 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]; } }