" />

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

iOS開(kāi)發(fā)技巧之自定義相機(jī)

 更新時(shí)間:2022年07月20日 15:29:27   作者:VKOOY  
這篇文章主要為大家詳細(xì)介紹了iOS開(kāi)發(fā)技巧之自定義相機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近公司的項(xiàng)目中用到了相機(jī),由于不用系統(tǒng)的相機(jī),UI給的相機(jī)切圖,必須自定義才可以。就花時(shí)間簡(jiǎn)單研究了一下相機(jī)的自定義。

相機(jī)屬于系統(tǒng)硬件,這就需要我們來(lái)手動(dòng)調(diào)用iPhone的相機(jī)硬件,分為以下步驟:

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

#import <AVFoundation/AVFoundation.h>
//捕獲設(shè)備,通常是前置攝像頭,后置攝像頭,麥克風(fēng)(音頻輸入)
@property (nonatomic, strong) AVCaptureDevice *device;
 
//AVCaptureDeviceInput 代表輸入設(shè)備,他使用AVCaptureDevice 來(lái)初始化
@property (nonatomic, strong) AVCaptureDeviceInput *input;
 
//輸出圖片
@property (nonatomic ,strong) AVCaptureStillImageOutput *imageOutput;
 
//session:由他把輸入輸出結(jié)合在一起,并開(kāi)始啟動(dòng)捕獲設(shè)備(攝像頭)
@property (nonatomic, strong) AVCaptureSession *session;
 
//圖像預(yù)覽層,實(shí)時(shí)顯示捕獲的圖像
@property (nonatomic ,strong) AVCaptureVideoPreviewLayer *previewLayer;

2、初始化各個(gè)對(duì)象

- (void)cameraDistrict
{
// AVCaptureDevicePositionBack 后置攝像頭
// AVCaptureDevicePositionFront 前置攝像頭
 self.device = [self cameraWithPosition:AVCaptureDevicePositionFront];
 self.input = [[AVCaptureDeviceInput alloc] initWithDevice:self.device error:nil];
 
 self.imageOutput = [[AVCaptureStillImageOutput alloc] init];
 
 self.session = [[AVCaptureSession alloc] init];
 //  拿到的圖像的大小可以自行設(shè)定
 // AVCaptureSessionPreset320x240
 // AVCaptureSessionPreset352x288
 // AVCaptureSessionPreset640x480
 // AVCaptureSessionPreset960x540
 // AVCaptureSessionPreset1280x720
 // AVCaptureSessionPreset1920x1080
 // AVCaptureSessionPreset3840x2160
 self.session.sessionPreset = AVCaptureSessionPreset640x480;
 //輸入輸出設(shè)備結(jié)合
 if ([self.session canAddInput:self.input]) {
  [self.session addInput:self.input];
 }
 if ([self.session canAddOutput:self.imageOutput]) {
  [self.session addOutput:self.imageOutput];
 }
 //預(yù)覽層的生成
 self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
 self.previewLayer.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64);
 self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
 [self.view.layer addSublayer:self.previewLayer];
 //設(shè)備取景開(kāi)始
 [self.session startRunning];
 if ([_device lockForConfiguration:nil]) {
 //自動(dòng)閃光燈,
  if ([_device isFlashModeSupported:AVCaptureFlashModeAuto]) {
   [_device setFlashMode:AVCaptureFlashModeAuto];
  }
  //自動(dòng)白平衡,但是好像一直都進(jìn)不去
  if ([_device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeAutoWhiteBalance]) {
   [_device setWhiteBalanceMode:AVCaptureWhiteBalanceModeAutoWhiteBalance];
  }
  [_device unlockForConfiguration];
 }
 
}

根據(jù)前后置位置拿到相應(yīng)的攝像頭:

- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position{
 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
 for ( AVCaptureDevice *device in devices )
  if ( device.position == position ){
   return device;
  }
 return nil;
}

3、拍照拿到相應(yīng)圖片:

- (void)photoBtnDidClick
{
 AVCaptureConnection *conntion = [self.imageOutput connectionWithMediaType:AVMediaTypeVideo];
  if (!conntion) {
   NSLog(@"拍照失敗!");
   return;
   }
 [self.imageOutput captureStillImageAsynchronouslyFromConnection:conntion completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
  if (imageDataSampleBuffer == nil) {
   return ;
   }
  NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
  self.image = [UIImage imageWithData:imageData];
  [self.session stopRunning];
  [self.view addSubview:self.cameraImageView];
}

4、保存照片到相冊(cè):

#pragma - 保存至相冊(cè)
- (void)saveImageToPhotoAlbum:(UIImage*)savedImage
{
 
 UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
 
}
// 指定回調(diào)方法
 
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
 
{
 NSString *msg = nil ;
 if(error != NULL){
  msg = @"保存圖片失敗" ;
 }else{
  msg = @"保存圖片成功" ;
 }
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存圖片結(jié)果提示"
           message:msg
           delegate:self
           cancelButtonTitle:@"確定"
           otherButtonTitles:nil];
 [alert show];
}

5、前后置攝像頭的切換

- (void)changeCamera{
 NSUInteger cameraCount = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count];
 if (cameraCount > 1) {
  NSError *error;
  //給攝像頭的切換添加翻轉(zhuǎn)動(dòng)畫(huà)
  CATransition *animation = [CATransition animation];
  animation.duration = .5f;
  animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  animation.type = @"oglFlip";
 
  AVCaptureDevice *newCamera = nil;
  AVCaptureDeviceInput *newInput = nil;
 //拿到另外一個(gè)攝像頭位置
  AVCaptureDevicePosition position = [[_input device] position];
  if (position == AVCaptureDevicePositionFront){
   newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];
   animation.subtype = kCATransitionFromLeft;//動(dòng)畫(huà)翻轉(zhuǎn)方向
  }
  else {
   newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];
   animation.subtype = kCATransitionFromRight;//動(dòng)畫(huà)翻轉(zhuǎn)方向
  }
  //生成新的輸入
  newInput = [AVCaptureDeviceInput deviceInputWithDevice:newCamera error:nil];
  [self.previewLayer addAnimation:animation forKey:nil];
  if (newInput != nil) {
   [self.session beginConfiguration];
   [self.session removeInput:self.input];
   if ([self.session canAddInput:newInput]) {
    [self.session addInput:newInput];
    self.input = newInput;
 
   } else {
    [self.session addInput:self.input];
   }
   [self.session commitConfiguration];
 
  } else if (error) {
   NSLog(@"toggle carema failed, error = %@", error);
  }
 }
}

6、相機(jī)的其它參數(shù)設(shè)置

//AVCaptureFlashMode 閃光燈
//AVCaptureFocusMode 對(duì)焦
//AVCaptureExposureMode 曝光
//AVCaptureWhiteBalanceMode 白平衡
//閃光燈和白平衡可以在生成相機(jī)時(shí)候設(shè)置
//曝光要根據(jù)對(duì)焦點(diǎn)的光線狀況而決定,所以和對(duì)焦一塊寫(xiě)
//point為點(diǎn)擊的位置
- (void)focusAtPoint:(CGPoint)point{
 CGSize size = self.view.bounds.size;
 CGPoint focusPoint = CGPointMake( point.y /size.height ,1-point.x/size.width );
 NSError *error;
 if ([self.device lockForConfiguration:&error]) {
  //對(duì)焦模式和對(duì)焦點(diǎn)
  if ([self.device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
   [self.device setFocusPointOfInterest:focusPoint];
   [self.device setFocusMode:AVCaptureFocusModeAutoFocus];
  }
  //曝光模式和曝光點(diǎn)
  if ([self.device isExposureModeSupported:AVCaptureExposureModeAutoExpose ]) {
   [self.device setExposurePointOfInterest:focusPoint];
   [self.device setExposureMode:AVCaptureExposureModeAutoExpose];
  }
 
  [self.device unlockForConfiguration];
  //設(shè)置對(duì)焦動(dòng)畫(huà)
  _focusView.center = point;
  _focusView.hidden = NO;
  [UIView animateWithDuration:0.3 animations:^{
   _focusView.transform = CGAffineTransformMakeScale(1.25, 1.25);
  }completion:^(BOOL finished) {
   [UIView animateWithDuration:0.5 animations:^{
    _focusView.transform = CGAffineTransformIdentity;
   } completion:^(BOOL finished) {
    _focusView.hidden = YES;
   }];
  }];
 }
 
}

7、遇到的一些坑和解決辦法

1) 前后置攝像頭的切換

前后值不能切換,各種嘗試找了半天沒(méi)找到有原因。后來(lái)發(fā)現(xiàn)我在設(shè)置圖片尺寸的時(shí)候設(shè)置為1080P [self.session canSetSessionPreset: AVCaptureSessionPreset1920x1080] ,前置攝像頭并不支持這么大的尺寸,所以就不能切換前置攝像頭。我驗(yàn)證了下 前置攝像頭最高支持720P,720P以內(nèi)可自由切換?! ?/p>

當(dāng)然也可以在前后置攝像頭切換的時(shí)候,根據(jù)前后攝像頭來(lái)設(shè)置不同的尺寸,這里不在贅述。

2)焦點(diǎn)位置

CGPoint focusPoint = CGPointMake( point.y /size.height ,1-point.x/size.width );
setExposurePointOfInterest:focusPoint 函數(shù)后面Point取值范圍是取景框左上角(0,0)到取景框右下角(1,1)之間。官方是這么寫(xiě)的:

  The value of this property is a CGPoint that determines the receiver's focus point of interest, if it has one. A value of (0,0) indicates that the camera should focus on the top left corner of the image, while a value of (1,1) indicates that it should focus on the bottom right. The default value is (0.5,0.5).

我也試了按這個(gè)來(lái)但位置就是不對(duì),只能按上面的寫(xiě)法才可以。前面是點(diǎn)擊位置的y/PreviewLayer的高度,后面是1-點(diǎn)擊位置的x/PreviewLayer的寬度

3)對(duì)焦和曝光

我在設(shè)置對(duì)焦是 先設(shè)置了模式setFocusMode,后設(shè)置對(duì)焦位置,就會(huì)導(dǎo)致很奇怪的現(xiàn)象,對(duì)焦位置是你上次點(diǎn)擊的位置。所以一定要先設(shè)置位置,再設(shè)置對(duì)焦模式。
曝光同上

8、寫(xiě)在最后

附上demo:photographDemo

常用到的基本就這么多,寫(xiě)的并不完善,有什么不對(duì)的,歡迎大家批評(píng)指正,共同學(xué)習(xí)。

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

相關(guān)文章

  • iOS判斷身份證號(hào)碼是否正確的方法

    iOS判斷身份證號(hào)碼是否正確的方法

    本篇文章主要介紹了iOS判斷身份證號(hào)碼是否正確的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • iOS tableView多輸入框如何獲取數(shù)據(jù)

    iOS tableView多輸入框如何獲取數(shù)據(jù)

    這篇文章主要給大家介紹了關(guān)于iOS tableView多輸入框如何獲取數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • iOS實(shí)現(xiàn)從背景圖中取色的代碼

    iOS實(shí)現(xiàn)從背景圖中取色的代碼

    這篇文章主要介紹了iOS實(shí)現(xiàn)從背景圖中取色的代碼,感興趣的小伙伴們可以參考一下
    2016-03-03
  • iOS動(dòng)畫(huà)解析之圓球加載動(dòng)畫(huà)XLBallLoading的實(shí)現(xiàn)

    iOS動(dòng)畫(huà)解析之圓球加載動(dòng)畫(huà)XLBallLoading的實(shí)現(xiàn)

    加載動(dòng)畫(huà)對(duì)大家來(lái)說(shuō)都不陌生,我們?cè)谄綍r(shí)都會(huì)遇見(jiàn),開(kāi)發(fā)中也必不可少,所以下面這篇文章主要給大家介紹了關(guān)于iOS動(dòng)畫(huà)解析之圓球加載動(dòng)畫(huà)XLBallLoading實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-11-11
  • iOS繪制3D餅圖的實(shí)現(xiàn)方法

    iOS繪制3D餅圖的實(shí)現(xiàn)方法

    餅圖常用于統(tǒng)計(jì)學(xué)模塊。常見(jiàn)的一般為2D餅圖,這篇文章主要介紹了iOS繪制3D餅圖的實(shí)現(xiàn)方法,3D餅圖更加立體,用戶的好感度也比較高,下面需要的朋友可以參考借鑒,一起來(lái)看看吧。
    2017-01-01
  • iOS中Swift UISearchController仿微信搜索框

    iOS中Swift UISearchController仿微信搜索框

    這篇文章主要介紹了iOS中Swift UISearchController仿微信搜索框效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-05-05
  • 關(guān)于iOS 11下app圖標(biāo)變空白問(wèn)題的解決方法

    關(guān)于iOS 11下app圖標(biāo)變空白問(wèn)題的解決方法

    升級(jí)到iOS11系統(tǒng)下自己的項(xiàng)目桌面app圖標(biāo)不見(jiàn)了,通過(guò)查找相關(guān)的資料終于找到了解決方法,下面這篇文章主要給大家介紹了關(guān)于iOS 11下app圖標(biāo)變空白問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2017-12-12
  • iOS鍵盤(pán)自適應(yīng)彈出效果

    iOS鍵盤(pán)自適應(yīng)彈出效果

    這篇文章主要為大家詳細(xì)介紹了iOS鍵盤(pán)自適應(yīng)彈出效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • IOS實(shí)現(xiàn)左右兩個(gè)TableView聯(lián)動(dòng)效果

    IOS實(shí)現(xiàn)左右兩個(gè)TableView聯(lián)動(dòng)效果

    在我們?nèi)粘i_(kāi)發(fā)IOS中,經(jīng)常見(jiàn)到兩個(gè)tableview的聯(lián)動(dòng),滑動(dòng)一側(cè)tableview,另一側(cè)tableview跟著滑動(dòng),其實(shí)實(shí)現(xiàn)起來(lái)比較簡(jiǎn)單,只是需要搞清楚他們之間的區(qū)別和聯(lián)系,下面一起來(lái)看看如何實(shí)現(xiàn)。
    2016-08-08
  • iOS提取APP中的圖片資源的方法

    iOS提取APP中的圖片資源的方法

    這篇文章主要介紹了iOS提取APP中的圖片資源的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11

最新評(píng)論

大连市| 麻城市| 永新县| 独山县| 缙云县| 阿拉善右旗| 阳春市| 兴安县| 特克斯县| 海兴县| 普洱| 东丰县| 车致| 石泉县| 马龙县| 新乡市| 米易县| 全南县| 门源| 保定市| 任丘市| 张家界市| 惠安县| 玉龙| 景泰县| 弥勒县| 肥西县| 巴塘县| 通辽市| 禹城市| 松原市| 舞阳县| 万山特区| 阳城县| 黎川县| 马山县| 珲春市| 汝南县| 浏阳市| 襄汾县| 大同县|