*我正在通过此代码捕获图像,但我还必须在单击按钮时录制视频并保存,所以请告诉我该怎么办?实际上我没有得到,因为我已成功捕获图像并将其保存到我的相册但现在我还必须录制视频并存储它所以请任何人给我更好的主意*
- (void)viewDidLoad
{
front=NO;
self.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
barPicker.value = hue;
pickerArr=[[NSArray alloc]initWithObjects:@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10", nil];
flashlightOn=YES;
[flashButton setImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateNormal];
[camraButton setImage:[UIImage imageNamed:@"camra.png"] forState:UIControlStateNormal];
session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;
CALayer *viewLayer = self.vImagePreview.layer;
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];
[self.vImagePreview addSubview:flashButton];
[self.vImagePreview addSubview:camraButton];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
if (!input) {
// Handle the error appropriately.
NSLog(@"ERROR: trying to open camera: %@", error);
}
else
{
[session addInput:input];
[stillImageOutput setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];
}
[session startRunning];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(IBAction) captureNow
{
if (!front)
{
if (flashlightOn) {
AVCaptureDevice *device1 = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device1 hasTorch] && [device1 hasFlash])
{
[device1 lockForConfiguration:nil];
[device1 setTorchMode:AVCaptureTorchModeOn];
[device1 setFlashMode:AVCaptureFlashModeOn];
[device1 unlockForConfiguration];
}
}
}
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) {
}
else {
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
image.image = [[UIImage alloc] initWithData:imageData];
}];
}