首先介紹下實現(xiàn)拍照和錄制視頻需要用到的類:
-
AVCaptureVideoPreviewLayer:捕獲視頻預(yù)覽層。 -
AVCaptureSession:捕獲會話類。 -
AVCaptureDevice:捕獲設(shè)備類。 -
AVCaptureDeviceInput:捕獲設(shè)備輸入類。 -
AVCapturePhotoOutput:捕獲照片輸出類。 -
AVCaptureMovieFileOutput:捕獲電影文件輸出類。 -
AVCaptureConnection:捕獲連接類。 -
AVCapturePhotoSettings:捕獲照片設(shè)置類。 -
AVAsset:資產(chǎn)類。 -
AVAssetImageGenerator:資產(chǎn)圖片生成器類。
首先來看下AVCaptureSession初始化的流程:

通過該流程圖可以看出,AVCaptureSession的初始化配置需要:
1、視頻輸入設(shè)備 。
2、音頻輸入設(shè)備。
3、照片輸出對象 。
3、電影文件輸出對象。
看核心代碼:
- (BOOL)setupSession:(NSError **)error {
self.captureSession = [[AVCaptureSession alloc] init];
self.captureSession.sessionPreset = AVCaptureSessionPresetHigh;
//視頻輸入設(shè)備
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *videoDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:videoDevice error:error];
if (videoDeviceInput) {
if ([self.captureSession canAddInput:videoDeviceInput]) {
[self.captureSession addInput:videoDeviceInput];
self.activeVideoInput = videoDeviceInput;
} else {
return NO;
}
} else {
return NO;
}
//音頻輸入設(shè)備
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput *audioDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:audioDevice error:error];
if (audioDeviceInput) {
if ([self.captureSession canAddInput:audioDeviceInput]) {
[self.captureSession addInput:audioDeviceInput];
} else {
return NO;
}
} else {
return NO;
}
//從實例攝像頭中捕捉靜態(tài)圖片
self.photoOutput = [[AVCapturePhotoOutput alloc] init];
if ([self.captureSession canAddOutput:self.photoOutput]) {
[self.captureSession addOutput:self.photoOutput];
}
//用于將電影錄制到文件系統(tǒng)
self.movieOutput = [[AVCaptureMovieFileOutput alloc] init];
if ([self.captureSession canAddOutput:self.movieOutput]) {
[self.captureSession addOutput:self.movieOutput];
}
self.videoQueue = dispatch_queue_create("CQCamera.Video.Queue", NULL);
return YES;
}
這段代碼最后我們還創(chuàng)建了個全局的串行隊列videoQueue,在后面開始捕獲和錄制時需要使用。
從上圖中我們還看到,在AVCaptureSession初始化配置結(jié)束后又做了兩個操作。
1、我們將AVCaptureVideoPreviewLayer的session設(shè)置為AVCaptureSession。
[(AVCaptureVideoPreviewLayer*)self.layer setSession:session];
- 將捕捉數(shù)據(jù)直接輸出到圖層中,并確保與會話狀態(tài)同步。
2、開始捕獲
- (void)startSession {
if (![self.captureSession isRunning]) {
dispatch_async(self.videoQueue, ^{
[self.captureSession startRunning];
});
}
}
下面看下如何將捕獲的內(nèi)容生產(chǎn)圖片。
一、拍照
同樣先看流程圖:

很明顯拍照我們需要使用AVCapturePhotoOutput 捕獲照片輸出對象。
看代碼:
- (void)captureStillImage {
AVCaptureConnection *connection = [self.photoOutput connectionWithMediaType:AVMediaTypeVideo];
if (connection.isVideoOrientationSupported) {
connection.videoOrientation = [self currentVideoOrientation];
}
self.photoSettings = [AVCapturePhotoSettings photoSettingsWithFormat:@{AVVideoCodecKey:AVVideoCodecTypeJPEG}];
[self.photoOutput capturePhotoWithSettings:self.photoSettings delegate:self];
}
- 拍照時我們需要拿到捕獲連接對象(
AVCaptureConnection),設(shè)置視頻的方向,否則在橫豎屏切換時會出現(xiàn)問題。 - 在代理方法中我們利用捕獲連接對象(
AVCaptureConnection)調(diào)用fileDataRepresentation方法獲取二進(jìn)制圖片。
獲取到圖片后需要利用Photos庫將圖片保存到相冊。
Photos
將圖片保存到相冊我們首先要判斷是否有權(quán)限,這個需要在plist文件中配置在這就不多說了。
下面我們來看下將圖片添加到指定相冊的流程:
第一步:添加圖片到【相機(jī)膠卷】。
1.1:UIImageWriteToSavedPhotosAlbum函數(shù)
1.2:AssetsLibrary框架(已過期,一般不用了)
1.3:Photos框架(推薦)第二步:擁有一個【自定義相冊】
2.1:AssetsLibrary框架
2.2:Photos框架(推薦)第三步:將剛才添加到【相機(jī)膠卷】的圖片,引用(添加)到【自定義相冊】
3.1:AssetsLibrary框架
3.2:Photos框架(推薦)
Photos框架相關(guān)類須知:
1、PHAsset:一個PHAsset對象代表一張圖片或者一個視頻文件。
負(fù)責(zé)查詢一堆的圖片或者視頻文件(PHAsset對象)。
2、PHAssetCollection:一個PHAssetCollection對象代表一個相冊。
負(fù)責(zé)查詢一堆的相冊(PHAssetCollection對象)。
3、PHAssetChangeRequest: 負(fù)責(zé)執(zhí)行對PHAsset(照片或視頻)的【增刪改】操作。
這個類只能放在-[PHPhotoLibrary performChanges:completionHandler:]或者 -[PHPhotoLibrary performChangesAndWait:error:]方法的block中使用。
4、PHAssetCollectionChangeRequest:負(fù)責(zé)執(zhí)行對PHAssetCollection(相冊)的【增刪改】操作。
這個類只能放在-[PHPhotoLibrary performChanges:completionHandler:] 或者 -[PHPhotoLibrary performChangesAndWait:error:]方法的block中使用。
- 保存圖片到 相機(jī)膠卷:
+ (PHFetchResult<PHAsset *> *)savePhoto:(UIImage *)image {
__block NSString *createdAssetId = nil;
// Synchronously 同步執(zhí)行操作
NSError *error;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
createdAssetId = [PHAssetChangeRequest creationRequestForAssetFromImage:image].placeholderForCreatedAsset.localIdentifier;
} error:&error];
if (error == nil) {
NSLog(@"保存成功");
} else {
NSLog(@"保存圖片Error: %@", error.localizedDescription);
return nil;
}
// // Asynchronously 異步執(zhí)行操作
// [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// [PHAssetChangeRequest creationRequestForAssetFromImage:image];
// } completionHandler:^(BOOL success, NSError * _Nullable error) {
// if (success) {
// NSLog(@"保存成功");
// } else {
// NSLog(@"保存圖片Error: %@", error.localizedDescription);
// }
// }];
//PHAsset:查詢圖片/視屏
PHFetchOptions *options = nil;
PHFetchResult<PHAsset *> *createdAssets = [PHAsset fetchAssetsWithLocalIdentifiers:@[createdAssetId] options:options];
return createdAssets;
}
- 獲取指定相冊:
+ (PHAssetCollection *)getAlbumWithTitle:(NSString *)title {
__block PHAssetCollection *createdCollection = nil;// 已經(jīng)創(chuàng)建的自定義相冊
//PHAssetCollection: 查詢所有的自定義相冊
PHFetchResult<PHAssetCollection *> *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
[collections enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL * _Nonnull stop) {
if ([collection.localizedTitle isEqualToString:title]) {
createdCollection = collection;
*stop = YES;
}
}];
if (!createdCollection) { // 沒有創(chuàng)建過相冊
__block NSString *createdCollectionId = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
//PHAssetCollectionChangeRequest:【增】相冊
createdCollectionId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title].placeholderForCreatedAssetCollection.localIdentifier;
} error:nil];
//PHAssetCollection:【查】出相冊
createdCollection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[createdCollectionId] options:nil].firstObject;
}
return createdCollection;
}
- 保存圖片到 指定相冊:
+ (BOOL)addAssetsToAlbumWithAssets:(id<NSFastEnumeration>)assets withAlbum:(PHAssetCollection *)assetCollection {
// 將剛才添加到【相機(jī)膠卷】的圖片,引用(添加)到【自定義相冊】
NSError *errorCollection = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
// 自定義相冊封面默認(rèn)保存第一張圖,所以使用以下方法把最新保存照片設(shè)為封面
[request insertAssets:assets atIndexes:[NSIndexSet indexSetWithIndex:0]];
} error:&errorCollection];
// 保存結(jié)果
if (errorCollection) {
NSLog(@"保存到指定 相冊 失??!");
return NO;
} else {
NSLog(@"保存到指定 相冊 成功!");
return YES;
}
}
二、錄頻
看下錄頻的操作:

錄頻核心代碼:
- (void)startRecording {
if (self.isRecording) return;
AVCaptureDevice *device = [self activeCamera];
//平滑對焦,減緩攝像頭對焦速度。移動拍攝時,攝像頭會嘗試快速對焦
if (device.isSmoothAutoFocusEnabled) {
NSError *error;
if ([device lockForConfiguration:&error]) {
device.smoothAutoFocusEnabled = YES;
[device unlockForConfiguration];
} else {
[self.delegate deviceConfigurationFailedWithError:error];
}
}
AVCaptureConnection *connection = [self.movieOutput connectionWithMediaType:AVMediaTypeVideo];
if (connection.isVideoOrientationSupported) {
connection.videoOrientation = [self currentVideoOrientation];
}
//判斷是否支持視頻穩(wěn)定。提高視頻的質(zhì)量。
if (connection.isVideoStabilizationSupported) {
connection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
}
self.outputURL = [self uniqueURL];
[self.movieOutput startRecordingToOutputFileURL:self.outputURL recordingDelegate:self];
}
- 1、我們首先需要拿到設(shè)置
AVCaptureSession是創(chuàng)建的 視頻捕獲設(shè)備輸入(AVCaptureDeviceInput),然后取出設(shè)備(AVCaptureDevice),配置設(shè)備的平滑對焦屬性。 - 2、然后同樣需要拿到捕獲連接對象(AVCaptureConnection),設(shè)置視頻的方向,否則在橫豎屏切換時會出現(xiàn)問題。并且需要設(shè)置
preferredVideoStabilizationMode屬性提高視頻的質(zhì)量。 - 3、調(diào)用
startRecordingToOutputFileURL:recordingDelegate:方法開始錄屏。 - 4、停止錄屏。
- 5、錄屏結(jié)束后在代理方法中獲取到我們的視頻地址。
錄屏結(jié)束后我們可能需要獲取視頻的某一幀圖片,用來顯示到UI上??聪虏僮鞑襟E:

流程圖很簡單,看下代碼:
//生成視頻縮略圖
- (void)generateThumbnailForVideoAtURL:(NSURL *)videoURL {
dispatch_async(self.videoQueue, ^{
AVAsset *asset = [AVAsset assetWithURL:videoURL];
AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
//設(shè)置maximumSize 寬為100,高為0 根據(jù)視頻的寬高比來計算圖片的高度
imageGenerator.maximumSize = CGSizeMake(100.0, 0.0);
//捕捉視頻縮略圖會考慮視頻的變化(如視頻的方向變化),如果不設(shè)置,縮略圖的方向可能出錯.
imageGenerator.appliesPreferredTrackTransform = YES;
NSError *error;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:kCMTimeZero actualTime:NULL error:&error];
if (imageRef == nil) {
NSLog(@"imageRefError: %@", error);
}
UIImage *image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
});
}
到此我們的拍照和錄屏的核心功能已經(jīng)實現(xiàn)了。下面介紹一下跟拍照錄屏相關(guān)的一些功能:切換攝像頭、聚焦、曝光、閃光燈、手電筒。
切換攝像頭
我們現(xiàn)在的手機(jī)設(shè)備一般都有前置和后置攝像頭,所以我們這里就是對前置和后置攝像頭的切換。
- (BOOL)switchCameras {
AVCaptureDevice *currentDevice = [self activeCamera];
AVCaptureDevice *device;
if (currentDevice.position == AVCaptureDevicePositionBack) {
device = [self cameraWithPosition:AVCaptureDevicePositionFront];
} else {
device = [self cameraWithPosition:AVCaptureDevicePositionBack];
}
if (device == nil) { return NO; }
NSError *error;
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (deviceInput) {
[self.captureSession beginConfiguration];
[self.captureSession removeInput:self.activeVideoInput];
if ([self.captureSession canAddInput:deviceInput]) {
[self.captureSession addInput:deviceInput];
self.activeVideoInput = deviceInput;
} else {
[self.captureSession addInput:self.activeVideoInput];
}
//配置完成后. 會分批的將所有變更整合在一起。
[self.captureSession commitConfiguration];
return YES;
} else {
[self.delegate deviceConfigurationFailedWithError:error];
return NO;
}
}
- 1、先拿到攝像頭設(shè)備
device。 - 2、將攝像頭包裝到
AVCaptureDeviceInput類型的對象中。 - 3、一定要先調(diào)用
beginConfiguration方法,準(zhǔn)備配置。 - 4、
removeInput:移除原來的捕獲設(shè)備輸入對象(`AVCaptureDeviceInput )。 - 5、判斷能否添加
canAddInput:新的捕獲設(shè)備輸入對象(`AVCaptureDeviceInput )。 - 6、如果可以就添加
addInput:,設(shè)置為當(dāng)前正在使用的捕獲設(shè)備輸入對象。 - 7、如果不可以添加,再將原來的捕獲設(shè)備輸入對象(`AVCaptureDeviceInput )添加進(jìn)去。
- 8、最后調(diào)用
commitConfiguration方法,分批的將所有變更整合在一起。
獲取前置或后置攝像頭的代碼:
- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position {
//AVCaptureDeviceTypeBuiltIn Microphone:話筒
//AVCaptureDeviceTypeBuiltIn WideAngleCamera:廣角照相機(jī)
//AVCaptureDeviceTypeBuiltIn TelephotoCamera:長焦照相機(jī)
//AVCaptureDeviceTypeBuiltIn UltraWideCamera:超寬攝影機(jī)
//AVCaptureDeviceTypeBuiltIn DualCamera:雙攝像頭
//AVCaptureDeviceTypeBuiltIn DualWideCamera:雙寬攝像頭
//AVCaptureDeviceTypeBuiltIn TripleCamera:三重攝影機(jī)
//AVCaptureDeviceTypeBuiltIn TrueDepthCamera:真深度照相機(jī)
//AVCaptureDeviceTypeBuiltIn DuoCamera:雙后置攝像頭
NSArray<AVCaptureDeviceType> *deviceTypes =@[
AVCaptureDeviceTypeBuiltInMicrophone,
AVCaptureDeviceTypeBuiltInTelephotoCamera,
AVCaptureDeviceTypeBuiltInWideAngleCamera,
AVCaptureDeviceTypeBuiltInDualCamera
];
AVCaptureDeviceDiscoverySession *deviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:deviceTypes mediaType:AVMediaTypeVideo position:position];
return deviceDiscoverySession.devices.firstObject;
}
聚焦 & 曝光
- 聚焦
- (void)focusAtPoint:(CGPoint)point {
AVCaptureDevice *device = [self activeCamera];
//是否支持興趣點聚焦 和 自動聚焦
if (device.isFocusPointOfInterestSupported && [device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
NSError *error;
if ([device lockForConfiguration:&error]) {//鎖定設(shè)備
device.focusPointOfInterest = point;//聚焦點
device.focusMode = AVCaptureFocusModeAutoFocus;//設(shè)置為自動聚焦
[device unlockForConfiguration];//解鎖設(shè)備
}
}
}
- 曝光
- (void)exposeAtPoint:(CGPoint)point {
AVCaptureDevice *device = [self activeCamera];
AVCaptureExposureMode exposureMode = AVCaptureExposureModeContinuousAutoExposure;
//是否支持興趣點曝光 和 持續(xù)自動曝光。
if (device.isExposurePointOfInterestSupported && [device isExposureModeSupported:exposureMode]) {
NSError *error;
if ([device lockForConfiguration:&error]) {
//配置期望值
device.exposurePointOfInterest = point;
device.exposureMode = exposureMode;
//判斷設(shè)備是否支持鎖定曝光的模式。
if ([device isExposureModeSupported:AVCaptureExposureModeLocked]) {
//支持,則使用kvo確定設(shè)備的adjustingExposure屬性的狀態(tài)。
[device addObserver:self forKeyPath:@"adjustingExposure" options:NSKeyValueObservingOptionNew context:&THCameraAdjustingExposureContext];
}
[device unlockForConfiguration];
}
}
}
這里曝光用到了kvo進(jìn)行監(jiān)聽屬性的狀態(tài):
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (context == &THCameraAdjustingExposureContext) {
AVCaptureDevice *device = (AVCaptureDevice *)object;
//判斷設(shè)備是否不再調(diào)整曝光等級,
//確認(rèn)設(shè)備的exposureMode是否可以設(shè)置為AVCaptureExposureModeLocked
if(!device.isAdjustingExposure && [device isExposureModeSupported:AVCaptureExposureModeLocked]) {
//移除作為adjustingExposure 的self,就不會得到后續(xù)變更的通知
[object removeObserver:self forKeyPath:@"adjustingExposure" context:&THCameraAdjustingExposureContext];
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error;
if ([device lockForConfiguration:&error]) {
//修改exposureMode
device.exposureMode = AVCaptureExposureModeLocked;
[device unlockForConfiguration];
}
});
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
閃光燈 & 手電筒
- 閃光燈
- (void)setFlashMode:(AVCaptureFlashMode)flashMode {
if ([self.photoOutput.supportedFlashModes containsObject:@(flashMode)]) {
self.photoSettings.flashMode = flashMode;
}
}
- 手電筒
- (void)setTorchMode:(AVCaptureTorchMode)torchMode {
AVCaptureDevice *device = [self activeCamera];
if ([device isTorchModeSupported:torchMode]) {
NSError *error;
if ([device lockForConfiguration:&error]) {
device.torchMode = torchMode;
[device unlockForConfiguration];
}
}
}