在不使用 uiimagepicker 控制器的情况下捕获图像和视频
Posted
技术标签:
【中文标题】在不使用 uiimagepicker 控制器的情况下捕获图像和视频【英文标题】:Capture image & Video without using uiimagepicker controller 【发布时间】:2012-07-11 11:15:53 【问题描述】:我需要在不打开 imagepickerController 的情况下捕获图像和视频。
【问题讨论】:
这个问题可能会有所帮助:***.com/questions/3457774/… 【参考方案1】:您可以使用 AVCaptureSession 捕获视频和照片
参考iPhone SDK 4 AVFoundation - How to use captureStillImageAsynchronouslyFromConnection correctly?
-(void) viewDidAppear:(BOOL)animated
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;
CALayer *viewLayer = self.vImagePreview.layer;
NSLog(@"viewLayer = %@", viewLayer);
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input)
// Handle the error appropriately.
NSLog(@"ERROR: trying to open camera: %@", error);
[session addInput:input];
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];
[session startRunning];
-(IBAction) captureNow
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;
NSLog(@"about to request a capture from: %@", stillImageOutput);
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments)
// Do something with the attachments.
NSLog(@"attachements: %@", exifAttachments);
else
NSLog(@"no attachments");
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
self.vImage.image = image;
];
【讨论】:
以上是关于在不使用 uiimagepicker 控制器的情况下捕获图像和视频的主要内容,如果未能解决你的问题,请参考以下文章
在选项卡式 UIPopover 中使用 UIImagePicker