AVCaptureMovieFileOutput 视频方向(锁定竖屏or不锁定都可以)
生活随笔
收集整理的這篇文章主要介紹了
AVCaptureMovieFileOutput 视频方向(锁定竖屏or不锁定都可以)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
最近又在弄短視頻錄制,解決一個問題又來一個問題。還好百度跟谷歌都是那么滴強大,基本上都能解決我遇到的問題。不過別人的答案不一定就是完全正確的答案,可能在自己的工程中就不起作用,所以還是需要我們自己去調試,發現問題,然后解決。 視頻錄制,我們需要根據設備方向來確定輸出視頻方向。比如橫屏錄制,如果不調整,那錄出來的方向就不對。查看類屬性,發現videoOrientation,給它賦予正確的方向值就可以。這個值怎么獲取呢? 方法一:[[UIApplication sharedApplication]statusBarOrientation] 方法二:[[UIDevice currentDevice]orientation];//監聽設備 [[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];//在合適的地方加上這行代碼上面方法在不鎖定豎屏方向都是有效的,一旦鎖屏,監聽就不起作用。那該怎么辦呢?別急,我們還有一個最合適的好方法。那就是CoreMotion,先#import <CoreMotion/CoreMotion.h>,然后 CMMotionManager *motionManager;(設為私有變量或者屬性,只是臨時初始化得到一個實例時不起作用的)。
motionManager = [[CMMotionManager alloc]init];motionManager.accelerometerUpdateInterval = .2;motionManager.gyroUpdateInterval = .2;[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {if(!error){[self outputData:accelerometerData.acceleration];}else{NSLog(@"%@",error);}}]; -(void)outputData:(CMAcceleration)data{UIInterfaceOrientation orientation;if(data.x >= 0.75){orientation = UIInterfaceOrientationLandscapeLeft;}else if (data.x<= -0.75){orientation = UIInterfaceOrientationLandscapeRight;}else if (data.y <= -0.75){orientation = UIInterfaceOrientationPortrait;}else if (data.y >= 0.75){orientation = UIInterfaceOrientationPortraitUpsideDown;}else{return;}orientationLast = orientation;}最后,在你點擊按鈕開始錄制的地方添加下面幾行代碼:
AVCaptureConnection *captureConnection = [self.fileOut connectionWithMediaType:AVMediaTypeVideo];captureConnection.videoOrientation = [self.captureVideoPreviewLayer connection].videoOrientation;[self.fileOut startRecordingToOutputFileURL:url recordingDelegate:self];轉載于:https://my.oschina.net/NycoWang/blog/1552814
總結
以上是生活随笔為你收集整理的AVCaptureMovieFileOutput 视频方向(锁定竖屏or不锁定都可以)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS 基础:文本和字体(4)思维导图
- 下一篇: dede 会员中心编辑添加和修改图集的时