SlideShare a Scribd company logo
1 of 21
Download to read offline
Capturing with
AVFoundation
2014/03/12 iOS_LT #8
@Tomoya_Onishi
自己紹介
• iOS開発歴約3年
• ツイート専用アプリ「FasPos」:累計5万DL
• その他位置情報ログアプリなどいくつか
AVFoundation
@import AVFoundation;
音声、画像、動画
の再生や作成の細かい作業を行うための
超強力な低レベルObjective-C API
今回はキャプチャ機能を
かなりざっくりと
AVCaptureSession
入力と出力を管理する
カメラ
マイク
静止画
音声データ
動画
メタデータ
AVCaptureDeviceInput
AVCaptureDevice
AVCaptureOutput
…
AVCaptureSession
• デバイスからの入力と出力を管理するクラス
@property(nonatomic, copy) NSString *sessionPreset;
キャプチャクオリティの調整
AVCaptureSessionPresetPhoto
AVCaptureSessionPresetHigh
AVCaptureSessionPresetMedium …
self.session = [[AVCaptureSession alloc] init];
AVCaptureDevice
• デバイスそのものを表現するクラス
self.camera =
[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
@property(nonatomic) AVCaptureFocusMode focusMode;
@property(nonatomic) CGPoint focusPointOfInterest;
…
@property(nonatomic, readonly) AVCaptureDevicePosition position;
@property(nonatomic) AVCaptureExposureMode exposureMode;
@property(nonatomic) AVCaptureWhiteBalanceMode whiteBalanceMode;
AVCaptureDeviceInput
• 指定したデバイスをセッションに入力する時に使うクラス
self.cameraInput =
[[AVCaptureDeviceInput alloc] initWithDevice:self.camera error:NULL];
[self.session addInput:self.cameraInput];
セッションにカメラが入力機器として接続された状態
AVCaptureOutput
• AVCaptureStillImageOutput(静止画)
• AVCaptureMovieFileOutput(動画)
• AVCaptureVideoDataOutput(ビデオデータ)
• AVCaptureAudioDataOutput(音声データ)
• AVCaptureMetadataOutput(顔、QRコードなど)
[self.session addOutput:self.output];
セッションへの入力を特定の方法で出力できる状態
AVCaptureStillImageOutput
• カメラから入力されたデータを静止画として出力するクラス
self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
@property(nonatomic)
BOOL automaticallyEnablesStillImageStabilizationWhenAvailable
                             NS_AVAILABLE_IOS(7_0)
手ぶれ補正ON
[self.session addOutput:self.stillImageOutput];
静止画のキャプチャ準備完了
- (void)captureStillImageAsynchronouslyFromConnection:(AVCaptureConnection *)connection
                      completionHandler:
       (void (^)(CMSampleBufferRef imageDataSampleBuffer, NSError *error))handler;
+ (NSData *)jpegStillImageNSDataRepresentation:(CMSampleBufferRef)jpegSampleBuffer;
JPEG画像がNSDataで取得できる
このメソッドを呼ぶと自動でシャッター音が鳴る
Exifなどのメタデータも含まれる
非同期で静止画をキャプチャ
CoreMediaのままではUIKitで使いづらいので変換
AVCaptureMovieFileOutput
• 動画を簡単にキャプチャできるクラス
self.movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
self.movieFileOutput.maxRecordedDuration = CMTimeMake(10 * 30, 30);
最大10秒キャプチャする
[self.session addOutput:self.movieFileOutput];
動画のキャプチャ準備完了
キャプチャ開始
[self.movieFileOutput startRecordingToOutputFileURL:self.movieURL    
                    recordingDelegate:self];
キャプチャ終了
self.movieFileOutput stopRecording];
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput           
             didStartRecordingToOutputFileAtURL:(NSURL *)fileURL
                   fromConnections:(NSArray *)connections
AVCaptureFileOutputRecordingDelegate
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput            
     didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
                 fromConnections:(NSArray *)connections
                        error:(NSError *)error
AVCaptureFileOutputRecordingDelegate
outputFileURLに動画が保存されている
AVCaptureVideoDataOutput
• 映像の各フレームをそのまま取得できるクラス
self.videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
self.videoDataOutput.videoSettings =
@{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };
self.videoDataQueue =
  dispatch_queue_create("jp.co.xxx.videoDataQueue", DISPATCH_QUEUE_SERIAL);
[self.videoDataOutput setSampleBufferDelegate:self queue:self.videoDataQueue];
[self.session addOutput:self.videoDataOutput];
映像フレームのキャプチャ準備完了
AVCaptureVideoDataOutputSampleBufferDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
sampleBufferがフレーム
1秒に何十回も呼ばれるので重い処理はしないように
各フレームの取得
プレビュー
AVCaptureVideoPreviewLayer *previewLayer =
[[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
previewLayer.frame = self.view.bounds;
セッションからレイヤーを生成して画面に貼り付けるだけ
盗撮防止のため?renderInContextが効かないようになっている
セッションを動かす
入出力をセッションに接続した状態で呼ぶ
実際に入力データがセッションに入って、出力できるようになる
[self.session startRunning];
[self.session stopRunning];
止める
Tips
• AVCaptureSession startRunning
• AVCaptureVideoPreviewLayerの生成が遅い
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
AVCaptureVideoPreviewLayer *previewLayer =
[[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
[self.session startRunning];
!
dispatch_async(dispatch_get_main_queue(), ^{
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
previewLayer.frame = frame;
});
!
});
非同期で
その他
• QRコードのキャプチャ及び生成

AVCaptureMetadataOutput, CIFilter
• 動画の再構成や画像との合成など

AVMutableComposition, AVVideoCompositionCoreAnimationTool
• 動画変換

AVAssetExportSession
• フレームから動画構築

AVAssetWriter
参考
• https://developer.apple.com/jp/devcenter/ios/library/
documentation/AVFoundationPG.pdf#search='AVFoundation'
• https://developer.apple.com/library/ios/navigation/
• ヘッダーファイル

More Related Content

Viewers also liked

ツイッター調査:約173万ツイートを調査して分かったTwitterの利用動向 #twtr_hack
ツイッター調査:約173万ツイートを調査して分かったTwitterの利用動向 #twtr_hackツイッター調査:約173万ツイートを調査して分かったTwitterの利用動向 #twtr_hack
ツイッター調査:約173万ツイートを調査して分かったTwitterの利用動向 #twtr_hackteapipin
 
OpenCVをAndroidで動かしてみた
OpenCVをAndroidで動かしてみたOpenCVをAndroidで動かしてみた
OpenCVをAndroidで動かしてみた徹 上野山
 
Photos vs Assets Library - いまさら始めるPhotos.framework
Photos vs Assets Library - いまさら始めるPhotos.frameworkPhotos vs Assets Library - いまさら始めるPhotos.framework
Photos vs Assets Library - いまさら始めるPhotos.frameworkKaname Noto
 
画像処理ライブラリ OpenCV で 出来ること・出来ないこと
画像処理ライブラリ OpenCV で 出来ること・出来ないこと画像処理ライブラリ OpenCV で 出来ること・出来ないこと
画像処理ライブラリ OpenCV で 出来ること・出来ないことNorishige Fukushima
 
IBDesignable / IBInspectable で UIプロトタイピンガブル
IBDesignable / IBInspectable で UIプロトタイピンガブルIBDesignable / IBInspectable で UIプロトタイピンガブル
IBDesignable / IBInspectable で UIプロトタイピンガブルMasaki Oshikawa
 
Proactive Suggestions
Proactive SuggestionsProactive Suggestions
Proactive SuggestionsGaprot
 
iOS 10 new Camera
iOS 10 new CameraiOS 10 new Camera
iOS 10 new CameraGaprot
 
日本らしいスタートアップエコシステムの新しい形
日本らしいスタートアップエコシステムの新しい形日本らしいスタートアップエコシステムの新しい形
日本らしいスタートアップエコシステムの新しい形Yagi Sohei
 
iOS 8/Swift 概要 #ios8yahoo
iOS 8/Swift 概要 #ios8yahooiOS 8/Swift 概要 #ios8yahoo
iOS 8/Swift 概要 #ios8yahooHiramatsu Ryosuke
 
Ios8yahoo swift-json
Ios8yahoo swift-jsonIos8yahoo swift-json
Ios8yahoo swift-jsondankogai
 
iOS8勉強会@Yahoo! JAPAN "Document Provider"
iOS8勉強会@Yahoo! JAPAN "Document Provider"iOS8勉強会@Yahoo! JAPAN "Document Provider"
iOS8勉強会@Yahoo! JAPAN "Document Provider"智也 大西
 
大人のHomekit
大人のHomekit大人のHomekit
大人のHomekitKen Haneda
 
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014Chris Adamson
 
もしiOS8のカスタムキーボードがガジェットのSDKを搭載したら Ver.#ios8yahoo
もしiOS8のカスタムキーボードがガジェットのSDKを搭載したら Ver.#ios8yahooもしiOS8のカスタムキーボードがガジェットのSDKを搭載したら Ver.#ios8yahoo
もしiOS8のカスタムキーボードがガジェットのSDKを搭載したら Ver.#ios8yahooniwatako
 
オプショナル型。 〜 なんとなく付ける ! ? 撲滅
オプショナル型。 〜 なんとなく付ける ! ? 撲滅オプショナル型。 〜 なんとなく付ける ! ? 撲滅
オプショナル型。 〜 なんとなく付ける ! ? 撲滅Tomoki Hasegawa
 
Stupid Video Tricks (CocoaConf DC, March 2014)
Stupid Video Tricks (CocoaConf DC, March 2014)Stupid Video Tricks (CocoaConf DC, March 2014)
Stupid Video Tricks (CocoaConf DC, March 2014)Chris Adamson
 
SwiftでSioriを開発した体験記
SwiftでSioriを開発した体験記SwiftでSioriを開発した体験記
SwiftでSioriを開発した体験記yohei sugigami
 
Amazon Web Servicesで未来へススメ!
Amazon Web Servicesで未来へススメ!Amazon Web Servicesで未来へススメ!
Amazon Web Servicesで未来へススメ!Genta Watanabe
 
Xcode 6の新機能
Xcode 6の新機能Xcode 6の新機能
Xcode 6の新機能Shingo Sato
 

Viewers also liked (20)

ツイッター調査:約173万ツイートを調査して分かったTwitterの利用動向 #twtr_hack
ツイッター調査:約173万ツイートを調査して分かったTwitterの利用動向 #twtr_hackツイッター調査:約173万ツイートを調査して分かったTwitterの利用動向 #twtr_hack
ツイッター調査:約173万ツイートを調査して分かったTwitterの利用動向 #twtr_hack
 
OpenCVの基礎
OpenCVの基礎OpenCVの基礎
OpenCVの基礎
 
OpenCVをAndroidで動かしてみた
OpenCVをAndroidで動かしてみたOpenCVをAndroidで動かしてみた
OpenCVをAndroidで動かしてみた
 
Photos vs Assets Library - いまさら始めるPhotos.framework
Photos vs Assets Library - いまさら始めるPhotos.frameworkPhotos vs Assets Library - いまさら始めるPhotos.framework
Photos vs Assets Library - いまさら始めるPhotos.framework
 
画像処理ライブラリ OpenCV で 出来ること・出来ないこと
画像処理ライブラリ OpenCV で 出来ること・出来ないこと画像処理ライブラリ OpenCV で 出来ること・出来ないこと
画像処理ライブラリ OpenCV で 出来ること・出来ないこと
 
IBDesignable / IBInspectable で UIプロトタイピンガブル
IBDesignable / IBInspectable で UIプロトタイピンガブルIBDesignable / IBInspectable で UIプロトタイピンガブル
IBDesignable / IBInspectable で UIプロトタイピンガブル
 
Proactive Suggestions
Proactive SuggestionsProactive Suggestions
Proactive Suggestions
 
iOS 10 new Camera
iOS 10 new CameraiOS 10 new Camera
iOS 10 new Camera
 
日本らしいスタートアップエコシステムの新しい形
日本らしいスタートアップエコシステムの新しい形日本らしいスタートアップエコシステムの新しい形
日本らしいスタートアップエコシステムの新しい形
 
iOS 8/Swift 概要 #ios8yahoo
iOS 8/Swift 概要 #ios8yahooiOS 8/Swift 概要 #ios8yahoo
iOS 8/Swift 概要 #ios8yahoo
 
Ios8yahoo swift-json
Ios8yahoo swift-jsonIos8yahoo swift-json
Ios8yahoo swift-json
 
iOS8勉強会@Yahoo! JAPAN "Document Provider"
iOS8勉強会@Yahoo! JAPAN "Document Provider"iOS8勉強会@Yahoo! JAPAN "Document Provider"
iOS8勉強会@Yahoo! JAPAN "Document Provider"
 
大人のHomekit
大人のHomekit大人のHomekit
大人のHomekit
 
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
 
もしiOS8のカスタムキーボードがガジェットのSDKを搭載したら Ver.#ios8yahoo
もしiOS8のカスタムキーボードがガジェットのSDKを搭載したら Ver.#ios8yahooもしiOS8のカスタムキーボードがガジェットのSDKを搭載したら Ver.#ios8yahoo
もしiOS8のカスタムキーボードがガジェットのSDKを搭載したら Ver.#ios8yahoo
 
オプショナル型。 〜 なんとなく付ける ! ? 撲滅
オプショナル型。 〜 なんとなく付ける ! ? 撲滅オプショナル型。 〜 なんとなく付ける ! ? 撲滅
オプショナル型。 〜 なんとなく付ける ! ? 撲滅
 
Stupid Video Tricks (CocoaConf DC, March 2014)
Stupid Video Tricks (CocoaConf DC, March 2014)Stupid Video Tricks (CocoaConf DC, March 2014)
Stupid Video Tricks (CocoaConf DC, March 2014)
 
SwiftでSioriを開発した体験記
SwiftでSioriを開発した体験記SwiftでSioriを開発した体験記
SwiftでSioriを開発した体験記
 
Amazon Web Servicesで未来へススメ!
Amazon Web Servicesで未来へススメ!Amazon Web Servicesで未来へススメ!
Amazon Web Servicesで未来へススメ!
 
Xcode 6の新機能
Xcode 6の新機能Xcode 6の新機能
Xcode 6の新機能
 

Recently uploaded

論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...Toru Tamaki
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものですiPride Co., Ltd.
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Yuma Ohgami
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムsugiuralab
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)Hiroki Ichikura
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdftaisei2219
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略Ryo Sasaki
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A surveyToru Tamaki
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNetToru Tamaki
 

Recently uploaded (9)

論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システム
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
 

AVFoundationを使ったキャプチャ機能