AVFoundation原理

标签:

avfoundation

it

分类: iphone开发
AVFoundation原理

例如,我要通过camera来获取一张静态图片(简单来说就是拍照),那么流程就应该是:
device(获取设备,这里为camera)-->capture input(用device初始化一个capture input);capture(初始化一个capture output)-->session(把input和output加入到session,然后startrunning)-->image(用out生成一个图片)
至于图片的生成方法,下面给出:

AVCaptureConnection *videoConnection = nil;

for (AVCaptureConnection *connection in imageOutput_.connections)

{

for (AVCaptureInputPort *port in [connectioninputPorts])

{

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

{

videoConnection = connection;  //先找出目标connections(见图)

break;

}

}

if (videoConnection) { break; }

}

//锁定connection后就可以获取静态图了。

[imageOutput_ captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRefimageSampleBuffer, NSError *error)

     {

NSDictionary *exifAttachments = (__bridgeNSDictionary*)CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary,NULL);

         NSData *imageData = [AVCaptureStillImageOutputjpegStillImageNSDataRepresentation:imageSampleBuffer];

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

}];


最后,如果想抓取视频的话,请参考苹果的文档:

 

Starting a Recording

You start recording a QuickTime movie using startRecordingToOutputFileURL:recordingDelegate:. You need to supply a file-based URL and a delegate. The URL must not identify an existing file, as the movie file output does not overwrite existing resources. You must also have permission to write to the specified location. The delegate must conform to the AVCaptureFileOutputRecordingDelegate protocol, and must implement thecaptureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error: method.

 AVCaptureMovieFileOutput *aMovieFileOutput = <#Get a movie file output#>;
 NSURL *fileURL = <#A file URL that identifies the output location#>;
 [aMovieFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:<#The delegate#>];

In the implementation of captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:, the delegate might write the resulting movie to the camera roll. It should also check for any errors that might have occurred.

Ensuring the File Was Written Successfully

To determine whether the file was saved successfully, in the implementation of captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error: you check not only the error, but also the value of theAVErrorRecordingSuccessfullyFinishedKey in the error’s user info dictionary:

 - (void)captureOutput:(AVCaptureFileOutput *)captureOutput
 didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
 fromConnections:(NSArray *)connections
 error:(NSError *)error {
 
 BOOL recordedSuccessfully = YES;
 if ([error code] != noErr) {
 // A problem occurred: Find out if the recording was successful.
 id value = [[error userInfo] objectForKey:AVErrorRecordingSuccessfullyFinishedKey];
 if (value) {
 recordedSuccessfully = [value boolValue];
 }
 }
 // Continue as appropriate...

You should check the value of the AVErrorRecordingSuccessfullyFinishedKey in the error’s user info dictionary because the file might have been saved successfully, even though you got an error. The error might indicate that one of your recording constraints was reached, for example AVErrorMaximumDurationReached or AVErrorMaximumFileSizeReached. Other reasons the recording might stop are:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值