iOS以编程方式拍照
我知道这是可能的,在一些应用程序中看到这个(iGotYa是我相信最有名的)。 我知道如何设置拍照的一切,节省一切。 但是,如何以编程的方式来完成呢? 只需要用户点击一些button(在常规的视图控制器),它会自动拍摄使用前置摄像头的图片并保存(或不,只是得到它作为UIImage)
谢谢!
这很简单,只需使用AVFoundation参考指南:
如果您不希望用户看到预览input,则可以跳过代码的设置预览图层部分。
编辑:要更详细。
1)您使用AVFoundation设置捕捉configuration。
- 将相机input设置为正面,closures闪光灯等。
2)跳过设置video预览图层的部分。
3)只要你想要拍照,你就调用captureStillImageAsynchronouslyFromConnection:completionHandler:方法。
注意:如果你不想听到闪光灯,那么你可能会侵犯一些国家(例如日本)的用户权利。 我知道这样做的一个解决方法是捕获一个video帧(不触发闪光)。
你也可以在没有AVFoundation的情况下做到这一点,在我看来,只有使用UIImagePickerController才能实现它。 有三个条件:
- 很明显,该设备需要有一个摄像头
- 您必须隐藏相机控件
- 然后简单地使用UIImagePickerController中的takePicture方法
下面是一个简单的例子,你通常会在按下button后触发
- (IBAction)takePhoto:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.cameraDevice = UIImagePickerControllerCameraDeviceFront; picker.showsCameraControls = NO; [self presentViewController:picker animated:YES completion:^ { [picker takePicture]; }]; }
VLBCameraView是一个使用AVFoundation拍照的库。
预览显示在视图中,然后可以以编程方式调用方法VLBCameraView#takePicture拍摄照片。
来与cocoa豆。
在.h文件中
@interface ABCViewController : UIViewController @property (strong, nonatomic) IBOutlet UIImageView *imageView; - (IBAction)takePhoto: (UIButton *)sender; - (IBAction)selectPhoto:(UIButton *)sender; @end
在.m文件中
@interface ABCViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> - (IBAction)takePhoto:(UIButton *)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; [self presentViewController:picker animated:YES completion:NULL]; } } - (IBAction)selectPhoto:(UIButton *)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:picker animated:YES completion:NULL]; } - (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]; }
这是Objective-C相机的代码。 你可以根据你的意愿添加function,button。
#import "CustomCameraVC.h" @interface CustomCameraVC () { BOOL frontCamera; } @property (strong,nonatomic) AVCaptureSession *captureSession; @property (strong,nonatomic) AVCaptureStillImageOutput *stillImageOutput; @property (strong,nonatomic) AVCaptureVideoPreviewLayer *videoPreviewLayer; @property (weak, nonatomic) IBOutlet UIView *viewCamera; @end @implementation CustomCameraVC - (void)viewDidLoad { [super viewDidLoad]; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:YES]; frontCamera = NO; [self showCameraWithFrontCamera:frontCamera]; } -(void)showCameraWithFrontCamera:(BOOL)flag { self.captureSession = [[AVCaptureSession alloc]init]; self.captureSession.sessionPreset = AVCaptureSessionPresetPhoto; AVCaptureDevice *captureDevice; if(flag) { captureDevice= [self frontCamera]; } else { captureDevice= [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; } NSError *error = nil; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error]; [self.captureSession addInput:input]; self.stillImageOutput = [AVCaptureStillImageOutput new]; self.stillImageOutput.outputSettings = @{AVVideoCodecKey:AVVideoCodecJPEG}; [self.captureSession addOutput:_stillImageOutput]; self.videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession]; self.videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; self.videoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait; [self.viewCamera.layer addSublayer:self.videoPreviewLayer]; [self.captureSession startRunning]; self.videoPreviewLayer.frame = self.viewCamera.bounds; } - (AVCaptureDevice *)frontCamera { NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice *device in devices) { if ([device position] == AVCaptureDevicePositionFront) { return device; } } return nil; } - (IBAction)btnCaptureImagePressed:(id)sender { AVCaptureConnection * videoConnection = [_stillImageOutput connectionWithMediaType:AVMediaTypeVideo]; [_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef _Nullable sampleBuffer, NSError * _Nullable error) { NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:sampleBuffer]; UIImage *image = [[UIImage alloc]initWithData: imageData]; UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); }]; } @end
- PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo不返回video
- swift:如何在不使用UIImagePickerController的情况下从照片库加载照片?
- 修改后的EXIF数据不能正确保存
- 打开相机卷在一张确切的照片
- 如何在转换为video的同时对图像进行animation处理
- 如何使用并使用UIImagePicker进行编辑 – UIImagePickerController.allowsEditing = YES
- 从摄像头获取所有图像asynchronous
- Swift照片库访问
- Swift 3:不使用UIImagePickerController从照片/相机胶卷中加载照片