最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS自定義相機(jī)實(shí)現(xiàn)拍照、錄制視頻

 更新時(shí)間:2019年04月21日 11:24:23   作者:有點(diǎn)糾結(jié)  
這篇文章主要為大家詳細(xì)介紹了iOS自定義相機(jī)實(shí)現(xiàn)拍照、錄制視頻,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了iOS自定義相機(jī)實(shí)現(xiàn)拍照、錄制視頻的具體代碼,供大家參考,具體內(nèi)容如下

使用AVFoundation框架。

這里是Demo

首先聲明以下對(duì)象:

#import "CustomeCameraViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
 
@interface CustomeCameraViewController ()<AVCaptureFileOutputRecordingDelegate>
 
{
  // AVCaptureSession對(duì)象來執(zhí)行輸入設(shè)備和輸出設(shè)備之間的數(shù)據(jù)傳遞
  AVCaptureSession *iSession;
  //當(dāng)前設(shè)備
  AVCaptureDevice *iDevice;
  //輸入設(shè)備
  AVCaptureDeviceInput *iDeviceInput;
  //照片輸出流
  AVCaptureStillImageOutput *iStillImageOutput;
  //預(yù)覽圖層
  AVCaptureVideoPreviewLayer *iPreviewLayer;
  
}

初始化各對(duì)象:

- (void)viewDidLoad {
  [super viewDidLoad];
  
  //點(diǎn)擊屏幕對(duì)焦
  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 后置攝像頭
    //AVCaptureDevicePositionFront 前置攝像頭
    if (device.position == AVCaptureDevicePositionBack) {
      iDevice = device;
    }
  }
  
  
  iSession.sessionPreset = [self getSessionPresetForDevice:iDevice];
  
  iDeviceInput = [[AVCaptureDeviceInput alloc]initWithDevice:iDevice error:nil];
  
  
  ////輸出設(shè)置。AVVideoCodecJPEG  輸出jpeg格式圖片
  iStillImageOutput = [[AVCaptureStillImageOutput alloc]init];
  NSDictionary *outputDic = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil];
  [iStillImageOutput setOutputSettings:outputDic];
  
  
  //更改這個(gè)設(shè)備設(shè)置的時(shí)候必須先鎖定設(shè)備,修改完后再解鎖,否則崩潰
  [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];
  }
  
  //初始化預(yù)覽圖層
  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];
  
  
}

點(diǎn)擊按鈕拍照:

//拍照
-(void)takePictures{
  AVCaptureConnection *connection = [iStillImageOutput connectionWithMediaType:AVMediaTypeVideo];
  if (!connection) {
    NSLog(@"失敗");
    return;
  }
  //設(shè)置焦距
  [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即為拍照所得圖片:

設(shè)置session的AVCaptureSessionPreset屬性

-(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;
  }
}

設(shè)置閃光燈:

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

切換前置攝像頭與后置攝像頭:

- (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 (newDeviceInput!=nil) {
    [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];
  }
  
  
}

點(diǎn)擊屏幕對(duì)焦:

//點(diǎn)擊屏幕對(duì)焦
-(void)focusTap:(UIGestureRecognizer *)tap{
  CGPoint tapPoint = [tap locationInView:self.view];
  
  
  float Y = tapPoint.y;
  if (Y<60 || 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;
    }];
  }];
}

關(guān)于視頻錄制可以在Demo中查看。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS 防鍵盤遮擋的實(shí)例

    iOS 防鍵盤遮擋的實(shí)例

    下面小編就為大家分享一篇iOS 防鍵盤遮擋的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • IOS實(shí)現(xiàn)聊天界面底部菜單欄效果

    IOS實(shí)現(xiàn)聊天界面底部菜單欄效果

    本文給大家分享的是放boss直聘當(dāng)中的聊天信息界面,主要思路是約束動(dòng)畫,實(shí)現(xiàn)代碼比較簡(jiǎn)單,下面小編通過本文給大家分享IOS實(shí)現(xiàn)聊天界面底部菜單欄效果,需要的的朋友參考下吧
    2017-09-09
  • iOS模仿微信長(zhǎng)按識(shí)別二維碼的多種方式

    iOS模仿微信長(zhǎng)按識(shí)別二維碼的多種方式

    這篇文章主要介紹了iOS模仿微信長(zhǎng)按識(shí)別二維碼的兩種方式,文章第二種方式是識(shí)別網(wǎng)頁中的二維碼,具體思路詳解大家參考下本文
    2017-07-07
  • iOS應(yīng)用開發(fā)中矢量圖的使用及修改矢量圖顏色的方法

    iOS應(yīng)用開發(fā)中矢量圖的使用及修改矢量圖顏色的方法

    這篇文章主要介紹了iOS應(yīng)用開發(fā)中矢量圖的使用及修改矢量圖顏色的方法,文中的方法是在Adobe Illustrator中繪制矢量圖然后導(dǎo)入Xcode中使用,需要的朋友可以參考下
    2016-03-03
  • IOS 實(shí)現(xiàn)搖一搖的操作

    IOS 實(shí)現(xiàn)搖一搖的操作

    這篇文章主要介紹了IOS 實(shí)現(xiàn)搖一搖的操作的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • iOS實(shí)現(xiàn)左右拖動(dòng)抽屜效果

    iOS實(shí)現(xiàn)左右拖動(dòng)抽屜效果

    這篇文章主要介紹了iOS實(shí)現(xiàn)左右拖動(dòng)抽屜效果,理解ios平臺(tái)類似于QQ主頁面,利用觸摸事件滑動(dòng)touchesMoved實(shí)現(xiàn)的效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • iOS通過逆向理解Block的內(nèi)存模型

    iOS通過逆向理解Block的內(nèi)存模型

    自從對(duì) iOS 的逆向初窺門徑后,我也經(jīng)常通過它來分析一些比較大的應(yīng)用,參考一下這些應(yīng)用中某些功能的實(shí)現(xiàn)。這個(gè)探索的過程樂趣多多,不僅能滿足自己對(duì)未知的好奇心,還經(jīng)常能發(fā)現(xiàn)一些意外的驚喜。這篇文章主要介紹了iOS通過逆向如何深入理解Block內(nèi)存模型的相關(guān)資料。
    2017-01-01
  • iOS開發(fā)避免安全隱患的要點(diǎn)總結(jié)

    iOS開發(fā)避免安全隱患的要點(diǎn)總結(jié)

    在本篇文章里小編給各位整理了關(guān)于iOS開發(fā)如何避免安全隱患的知識(shí)點(diǎn)總結(jié),需要的朋友們學(xué)習(xí)下。
    2019-07-07
  • iOS 中Swift仿微信添加提示小紅點(diǎn)功能(無數(shù)字)

    iOS 中Swift仿微信添加提示小紅點(diǎn)功能(無數(shù)字)

    這篇文章主要介紹了iOS 中Swift仿微信添加提示小紅點(diǎn)功能(無數(shù)字),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-05-05
  • 使用UItableview在iOS應(yīng)用開發(fā)中實(shí)現(xiàn)好友列表功能

    使用UItableview在iOS應(yīng)用開發(fā)中實(shí)現(xiàn)好友列表功能

    這篇文章主要介紹了使用UItableview在iOS應(yīng)用開發(fā)中實(shí)現(xiàn)一個(gè)好友列表功能的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-12-12

最新評(píng)論

信丰县| 临武县| 汤原县| 阿克苏市| 遵义县| 巴彦淖尔市| 安顺市| 平定县| 河池市| 花莲县| 景德镇市| 衡水市| 奉新县| 平塘县| 呼伦贝尔市| 安乡县| 衡东县| 隆安县| 泸州市| 浏阳市| 师宗县| 专栏| 湟源县| 讷河市| 读书| 二手房| 雷山县| 江华| 尚志市| 梨树县| 河西区| 绥江县| 开封市| 土默特左旗| 泊头市| 蒲城县| 新津县| 辽阳县| 观塘区| 九龙坡区| 治县。|