iOS自定義相機(jī)界面囱晴,實(shí)現(xiàn)微信小視頻UI效果

在開(kāi)發(fā)的時(shí)候屯掖,要實(shí)現(xiàn)微信小視頻的效果只用系統(tǒng)框架中的picker是不行的,它是個(gè)navi 而我們需要一個(gè)view把它放到tableview后邊

所以我們需要自定義一個(gè)攝像界面

下邊的文章摘抄自http://course.gdou.com/blog/Blog.pzs/archive/2011/12/14/10882.html

看完了相信對(duì)你會(huì)有很大的幫助

在進(jìn)行視頻捕獲時(shí)纬傲,有輸入設(shè)備及輸出設(shè)備满败,程序通過(guò) AVCaptureSession 的一個(gè)實(shí)例來(lái)協(xié)調(diào)、組織數(shù)據(jù)在它們之間的流動(dòng)叹括。

程序中至少需要:

●An instance ofAVCaptureDeviceto represent the input device, such as a camera or microphone

●An instance of a concrete subclass ofAVCaptureInputto configure the ports from the input device

●An instance of a concrete subclass ofAVCaptureOutputto manage the output to a movie file or still?image

●An instance ofAVCaptureSessionto coordinate the data flow from the input to the output

由上圖可以看出算墨,一個(gè)AVCaptureSession 可以協(xié)調(diào)多個(gè)輸入設(shè)備及輸出設(shè)備。通過(guò) AVCaptureSession 的 addInput汁雷、addOutput 方法可將輸入净嘀、輸出設(shè)備加入 AVCaptureSession 中。

capture input 及 capture out 之間的聯(lián)系由

AVCaptureConnection 對(duì)象表示侠讯,capture input (AVCaptureInput)

有一個(gè)或多個(gè)輸入端口(AVCaptureInputPort 實(shí)例) 挖藏,capture out(AVCaptureOutput

實(shí)例)可接收來(lái)自一個(gè)或多個(gè)輸入源的數(shù)據(jù)。

當(dāng)一個(gè)輸入或一個(gè)輸出被加入到 ?AVCaptureSession 中時(shí)厢漩,該 session 會(huì)“貪婪地” 在所有兼容的輸入端口和輸出之間建立連接(AVCaptureConnection)膜眠,因此,一般不需要手工在輸入溜嗜、輸出間建立連接宵膨。

輸入設(shè)備:

AVCaptureDeviceInput?*captureInput = [AVCaptureDeviceInput?deviceInputWithDevice:

[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo]error:nil];

種類(lèi)有:

AVMediaTypeVideo

AVMediaTypeAudio

輸出設(shè)備有:

AVCaptureMovieFileOutput 輸出到文件

AVCaptureVideoDataOutput 可用于處理被捕獲的視頻幀

AVCaptureAudioDataOutput 可用于處理被捕獲的音頻數(shù)據(jù)

AVCaptureStillImageOutput 可用于捕獲帶有元數(shù)據(jù)(MetaData)的靜止圖像

輸出設(shè)備對(duì)象的創(chuàng)建:

AVCaptureMovieFileOutput ?*captureOutput= [[AVCaptureMovieFileOutputalloc]init;

一、捕獲到視頻文件

此時(shí)輸出設(shè)備指定為:AVCaptureMovieFileOutput炸宵,其子類(lèi) AVCaptureFileOutput 的2個(gè)方法用于啟動(dòng)辟躏、停止編碼輸出:

- (void)startRecordingToOutputFileURL:(NSURL*)outputFileURLrecordingDelegate:(id <AVCaptureFileOutputRecordingDelegate>)delegate

- (void)stopRecording

程序開(kāi)始編碼輸出時(shí),應(yīng)先啟動(dòng) AVCaptureSession土全,再用以上方法啟動(dòng)編碼輸出捎琐。整個(gè)步驟:

創(chuàng)建輸入、輸出設(shè)備裹匙、AVCaptureSession對(duì)象:

AVCaptureDeviceInput*captureInput = [AVCaptureDeviceInputdeviceInputWithDevice:[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo]error:nil];

AVCaptureDeviceInput*microphone = [AVCaptureDeviceInputdeviceInputWithDevice:[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeAudio]error:nil];

/*We setupt the output*/

captureOutput= [[AVCaptureMovieFileOutputalloc]init];

self.captureSession= [[AVCaptureSessionalloc]init];

加入輸入瑞凑、輸出設(shè)備:

[self.captureSessionaddInput:captureInput];

[self.captureSessionaddInput:microphone];

[self.captureSessionaddOutput:self.captureOutput];

設(shè)置Session 屬性:

/*We use medium quality, ont the iPhone 4 this demo would be laging too much, the conversion in UIImage and CGImage demands too much ressources for a 720p resolution.*/

[self.captureSessionsetSessionPreset:AVCaptureSessionPresetMedium];

其他預(yù)置屬性如下:

不同設(shè)備的情況:

開(kāi)始編碼,視頻編碼為H.264幻件、音頻編碼為AAC:

[selfperformSelector:@selector(startRecording)withObject:nilafterDelay:10.0];

[self.captureSessionstartRunning];

- (void) startRecording

{

[captureOutputstartRecordingToOutputFileURL:[selftempFileURL]recordingDelegate:self];

}

處理編碼過(guò)程中事件的類(lèi)必須符合 VCaptureFileOutputRecordingDelegate 協(xié)議拨黔,并在以下2個(gè)方法中進(jìn)行處理:

- (void)captureOutput:(AVCaptureFileOutput*)captureOutput didStartRecordingToOutputFileAtURL:(NSURL*)fileURL fromConnections:(NSArray*)connections

{

NSLog(@"start record video");

}

- (void)captureOutput:(AVCaptureFileOutput*)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL*)outputFileURL fromConnections:(NSArray*)connections error:(NSError*)error

{

ALAssetsLibrary*library = [[ALAssetsLibraryalloc]init];

// 將臨時(shí)文件夾中的視頻文件復(fù)制到照片文件夾中,以便存取

[librarywriteVideoAtPathToSavedPhotosAlbum:outputFileURL

completionBlock:^(NSURL*assetURL,NSError*error) {

if(error) {

_myLabel.text=@"Error";

}

else

_myLabel.text= [assetURLpath];

}];

[libraryrelease];}

通過(guò) AVCaptureFileOutput ?的 stopRecording 方法停止編碼。

二篱蝇、捕獲用于處理視頻幀

三贺待、捕獲為靜止圖像

此時(shí)輸出設(shè)備對(duì)象為:AVCaptureStillImageOutput,session 的預(yù)置(preset)信息決定圖像分辨率:

圖像格式:

例:

AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc]?init];

NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:?AVVideoCodecJPEG,?AVVideoCodecKey, nil];

[stillImageOutput setOutputSettings:outputSettings];

當(dāng)需要捕獲靜止圖像時(shí)零截,可向輸出設(shè)備對(duì)象發(fā)送:captureStillImageAsynchronouslyFromConnection:completionHandler:

消息麸塞。第一個(gè)參數(shù)為欲進(jìn)行圖像捕獲的連接對(duì)象(AVCaptureConnection),你必須找到具有視頻采集輸入端口(port)的連接:

AVCaptureConnection *videoConnection = nil;

for (AVCaptureConnection *connection in stillImageOutput.connections) {

for (AVCaptureInputPort *port in [connection inputPorts]) {

if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {

videoConnection = connection;

break;

}

}

if (videoConnection) { break; }

}

第二個(gè)參數(shù)是一個(gè)塊(block)涧衙,它有2個(gè)參數(shù)哪工,第一個(gè)參數(shù)是包含圖像數(shù)據(jù)的??CMSampleBuffer,可用于處理圖像:

[[selfstillImageOutput]captureStillImageAsynchronouslyFromConnection:stillImageConnection

completionHandler:^(CMSampleBufferRefimageDataSampleBuffer,NSError*error) {

ALAssetsLibraryWriteImageCompletionBlockcompletionBlock = ^(NSURL*assetURL,NSError*error) {

if(error) {

// error handling

}

}

};

if(imageDataSampleBuffer !=NULL) {

NSData*imageData = [AVCaptureStillImageOutputjpegStillImageNSDataRepresentation:imageDataSampleBuffer];

ALAssetsLibrary*library = [[ALAssetsLibraryalloc]init];

UIImage*image = [[UIImagealloc]initWithData:imageData];

// 將圖像保存到“照片” 中

[librarywriteImageToSavedPhotosAlbum:[imageCGImage]?orientation:(ALAssetOrientation)[imageimageOrientation]

completionBlock:completionBlock];

[imagerelease];

[libraryrelease];

}

else

completionBlock(nil, error);

if([[selfdelegate]respondsToSelector:@selector(captureManagerStillImageCaptured:)]) {

[[selfdelegate]captureManagerStillImageCaptured:self];

}

}];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末弧哎,一起剝皮案震驚了整個(gè)濱河市雁比,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌撤嫩,老刑警劉巖偎捎,帶你破解...
    沈念sama閱讀 222,000評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異序攘,居然都是意外死亡茴她,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)程奠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)丈牢,“玉大人,你說(shuō)我怎么就攤上這事瞄沙〖号妫” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,561評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵帕识,是天一觀(guān)的道長(zhǎng)泛粹。 經(jīng)常有香客問(wèn)我遂铡,道長(zhǎng)肮疗,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,782評(píng)論 1 298
  • 正文 為了忘掉前任扒接,我火速辦了婚禮伪货,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘钾怔。我一直安慰自己碱呼,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布宗侦。 她就那樣靜靜地躺著愚臀,像睡著了一般。 火紅的嫁衣襯著肌膚如雪矾利。 梳的紋絲不亂的頭發(fā)上姑裂,一...
    開(kāi)封第一講書(shū)人閱讀 52,394評(píng)論 1 310
  • 那天馋袜,我揣著相機(jī)與錄音,去河邊找鬼舶斧。 笑死欣鳖,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的茴厉。 我是一名探鬼主播泽台,決...
    沈念sama閱讀 40,952評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼矾缓!你這毒婦竟也來(lái)了怀酷?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,852評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤嗜闻,失蹤者是張志新(化名)和其女友劉穎胰坟,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體泞辐,經(jīng)...
    沈念sama閱讀 46,409評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡笔横,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評(píng)論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了咐吼。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吹缔。...
    茶點(diǎn)故事閱讀 40,615評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖锯茄,靈堂內(nèi)的尸體忽然破棺而出厢塘,到底是詐尸還是另有隱情,我是刑警寧澤肌幽,帶...
    沈念sama閱讀 36,303評(píng)論 5 350
  • 正文 年R本政府宣布晚碾,位于F島的核電站,受9級(jí)特大地震影響喂急,放射性物質(zhì)發(fā)生泄漏格嘁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評(píng)論 3 334
  • 文/蒙蒙 一廊移、第九天 我趴在偏房一處隱蔽的房頂上張望糕簿。 院中可真熱鬧,春花似錦狡孔、人聲如沸懂诗。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,470評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)殃恒。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間离唐,已是汗流浹背隆嗅。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,571評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留侯繁,地道東北人胖喳。 一個(gè)月前我還...
    沈念sama閱讀 49,041評(píng)論 3 377
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像贮竟,于是被迫代替她去往敵國(guó)和親丽焊。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容