In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how iOS custom camera to achieve photography, recording video, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to know about it.
The details are as follows
Use the AVFoundation framework.
First declare the following objects:
# import "CustomeCameraViewController.h" # import # import @ interface CustomeCameraViewController () {/ / AVCaptureSession object to perform data transfer between input device and output device AVCaptureSession * iSession; / / current device AVCaptureDevice * iDevice; / / input device AVCaptureDeviceInput * iDeviceInput; / / Photo output stream AVCaptureStillImageOutput * iStillImageOutput; / / Preview layer AVCaptureVideoPreviewLayer * iPreviewLayer;}
Initialize each object:
-(void) viewDidLoad {[super viewDidLoad]; / / Click screen focus UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector (focusTap:)]; [self.view addGestureRecognizer:tap]; iSession = [[AVCaptureSession alloc] init]; NSArray * deviceArray = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice * device in deviceArray) {/ / AVCaptureDevicePositionBack rear camera / / AVCaptureDevicePositionFront front camera if (device.position = AVCaptureDevicePositionBack) {iDevice = device }} iSession.sessionPreset = [self getSessionPresetForDevice:iDevice]; iDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:iDevice error:nil]; / output settings. AVVideoCodecJPEG output jpeg format image iStillImageOutput = [[AVCaptureStillImageOutput alloc] init]; NSDictionary * outputDic = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil]; [iStillImageOutput setOutputSettings:outputDic]; / / when changing the settings of this device, you must lock the device first, modify it and then unlock it, otherwise crash [iDevice lockForConfiguration:nil]; if ([iDevice isFlashModeSupported:AVCaptureFlashModeOff]) {[iDevice setFlashMode:AVCaptureFlashModeOff];} if ([iDevice isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {[iDevice setFocusMode:AVCaptureFocusModeAutoFocus] } if ([iDevice isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeAutoWhiteBalance]) {[iDevice setWhiteBalanceMode:AVCaptureWhiteBalanceModeAutoWhiteBalance];} [iDevice unlockForConfiguration]; if ([iSession canAddInput:iDeviceInput]) {[iSession addInput:iDeviceInput];} if ([iSession canAddOutput:iStillImageOutput]) {[iSession addOutput:iStillImageOutput];} if ([iSession canAddOutput:iVideoOutput]) {[iSession addOutput:iVideoOutput];} / / initialize the preview layer iPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:iSession]; [iPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill] IPreviewLayer.frame = CGRectMake (0,60, [UIScreen mainScreen] .bounds.size.width, [UIScreen mainScreen] .bounds.size.height-160); [self.iCameraView.layer addSublayer:iPreviewLayer]; [iSession startRunning];}
Click the button to take a picture:
/ / take a picture-(void) takePictures {AVCaptureConnection * connection = [iStillImageOutput connectionWithMediaType:AVMediaTypeVideo]; if (! connection) {NSLog (@ "failure"); return;} / / set focal length [connection setVideoScaleAndCropFactor:1]; [iStillImageOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler: ^ (CMSampleBufferRef imageDataSampleBuffer, NSError * error) {if (imageDataSampleBuffer==NULL) {NSLog (@ "NUll"); return;} NSData * data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer] UIImage * image = [UIImage imageWithData:data];}];}
Image is the picture obtained by taking a photo:
Set the AVCaptureSessionPreset property of session
-(NSString *) getSessionPresetForDevice: (AVCaptureDevice *) device {if ([device supportsAVCaptureSessionPreset:AVCaptureSessionPreset3840x2160]) {return AVCaptureSessionPreset3840x2160;} else if ([device supportsAVCaptureSessionPreset:AVCaptureSessionPreset1920x1080]) {return AVCaptureSessionPreset1920x1080;} else if ([device supportsAVCaptureSessionPreset:AVCaptureSessionPreset1280x720]) {return AVCaptureSessionPreset1280x720;} else if ([device supportsAVCaptureSessionPreset:AVCaptureSessionPreset640x480]) {return AVCaptureSessionPreset640x480;} else if ([device supportsAVCaptureSessionPreset:AVCaptureSessionPreset352x288]) {return AVCaptureSessionPreset352x288;} else if ([device supportsAVCaptureSessionPreset:AVCaptureSessionPresetHigh]) {return AVCaptureSessionPresetHigh } else if ([device supportsAVCaptureSessionPreset:AVCaptureSessionPresetMedium]) {return AVCaptureSessionPresetMedium;} else {return AVCaptureSessionPresetLow;}}
Set up the flash:
-(IBAction) iFlashBtn: (id) sender {[iDevice lockForConfiguration:nil]; if (iDevice.flashMode = = AVCaptureFlashModeOff) {if ([iDevice isFlashModeSupported:AVCaptureFlashModeOn]) {[iDevice setFlashMode:AVCaptureFlashModeOn]; [self.iFlashBtn setBackgroundImage: [UIImage imageNamed:@ "flashBtn"] forState:UIControlStateNormal];} else if (iDevice.flashMode = = AVCaptureFlashModeOn) {if ([iDevice isFlashModeSupported:AVCaptureFlashModeOff]) {[iDevice setFlashMode:AVCaptureFlashModeOff] [self.iFlashBtn setBackgroundImage: [UIImage imageNamed:@ "flashOffBtn"] forState:UIControlStateNormal];}} [iDevice unlockForConfiguration];}
Switch between the front camera and the rear camera:
-(IBAction) iChangeBtn: (id) sender {NSArray * array = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; AVCaptureDevice * newDevice = nil; AVCaptureDeviceInput * newDeviceInput = nil; CATransition * animation = [CATransition animation]; animation.duration = 0.5f; animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; animation.type = @ "oglFlip"; if (iDevice.position = = AVCaptureDevicePositionBack) {animation.subtype = kCATransitionFromLeft; for (AVCaptureDevice * device in array) {if (device.position = = AVCaptureDevicePositionFront) {newDevice = device } else if (iDevice.position = = AVCaptureDevicePositionFront) {animation.subtype = kCATransitionFromRight; for (AVCaptureDevice * device in array) {if (device.position = = AVCaptureDevicePositionBack) {newDevice = device;}} newDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:newDevice error:nil]; [iPreviewLayer addAnimation:animation forKey:nil]; if (newDeviceInputCompanynil) {[iSession beginConfiguration]; [iSession removeInput:iDeviceInput]; iSession.sessionPreset = [self getSessionPresetForDevice:newDevice] If ([iSession canAddInput:newDeviceInput]) {[iSession addInput:newDeviceInput]; iDeviceInput = newDeviceInput; iDevice = newDevice;} else {[iSession addInput:iDeviceInput];} [iSession commitConfiguration];}}
Click the screen to focus:
/ / Click the screen focus-(void) focusTap: (UIGestureRecognizer *) tap {CGPoint tapPoint = [tap locationInView:self.view]; float Y = tapPoint.y; if (Y ([UIScreen mainScreen] .bounds.size.height-100)) {return;} [iDevice lockForConfiguration:nil]; if ([iDevice isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {[iDevice setFocusPointOfInterest:CGPointMake (tapPoint.x/self.view.frame.origin.x, tapPoint.y/self.view.frame.origin.y)] [iDevice setFocusMode:AVCaptureFocusModeAutoFocus];} [iDevice unlockForConfiguration]; self.iFocusImgView.center = tapPoint; self.iFocusImgView.hidden = NO; [UIView animateWithDuration:0.3 animations: ^ {self.iFocusImgView.transform = CGAffineTransformMakeScale (1.25,1.25);} completion: ^ (BOOL finished) {[UIView animateWithDuration:0.5 animations: ^ {self.iFocusImgView.transform = CGAffineTransformIdentity;} completion: ^ (BOOL finished) {self.iFocusImgView.hidden = YES;}] Thank you for reading this article carefully. I hope the article "how to customize iOS camera to take photos and record videos" shared by the editor will be helpful to you. At the same time, I also hope you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.