简体   繁体   English

如何使用AVCaptureDevice保存图像

[英]How to save image using AVCaptureDevice

I'm using Apple's GLCameraRipple example code in my project and I was wondering if there is a way to take a pic/video? 我在我的项目中使用Apple的GLCameraRipple示例代码,我想知道是否可以拍摄照片/视频? Project can be found at https://developer.apple.com/library/ios/samplecode/GLCameraRipple/Introduction/Intro.html 可以在https://developer.apple.com/library/ios/samplecode/GLCameraRipple/Introduction/Intro.html上找到该项目。

First of all, ensure that you have included AssetLibrary framework, because we need that to access the photo library. 首先,请确保您已包含AssetLibrary框架,因为我们需要该框架来访问照片库。 Assuming that you have set up the capture AVCaptureSession, AVCaptureDevice, and AVCaptureStillImageOutput (stillImage) correctly, now you can create a button or simply call the below function to save an image. 假设您已正确设置捕获AVCaptureSession,AVCaptureDevice和AVCaptureStillImageOutput(stillImage),现在可以创建一个按钮或简单地调用以下函数来保存图像。

-(void)captureMultipleTimes
{
AVCaptureConnection *connection = [stillImage connectionWithMediaType:AVMediaTypeVideo];

typedef void(^MyBufBlock)(CMSampleBufferRef, NSError*);

MyBufBlock h = ^(CMSampleBufferRef buf, NSError *err){
    NSData *data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:buf];
    [self setToSaveImage:[UIImage imageWithData:data]];

    dispatch_async(dispatch_get_main_queue(), ^{
        if(saveLabel == NULL){
            [self setSaveLabel:[[UILabel alloc] initWithFrame:CGRectMake(0,self.view.bounds.size.height/2, self.view.bounds.size.width, 50)]];
            [saveLabel setText:@"Saving.."];
            [saveLabel setTextColor:[captureBt titleColorForState:UIControlStateNormal]];
            [self.view addSubview:saveLabel];
        } else
            saveLabel.hidden = NO;

        UIImageWriteToSavedPhotosAlbum(toSaveImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    });
};
[stillImage captureStillImageAsynchronouslyFromConnection:connection completionHandler:h];
}

You also need to implement the image:didFinishSavingWithError:contextInfo: method as the completion function of saving image. 您还需要实现image:didFinishSavingWithError:contextInfo:方法作为保存图像的完成功能。 One example is as follows: 一个示例如下:

-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if(error != NULL){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Image could not be saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}
else{
    [saveLabel setHidden:YES];
}
}

The functions above will display "Saving.." label on the screen once you trigger the captureMultipleTimes function. 触发captureMultipleTimes函数后,以上函数将在屏幕上显示“ Saving ..”标签。 It simply means that it is currently saving the video input as an image and store it into the photo lib. 这仅表示它当前正在将视频输入另存为图像并将其存储到照片库中。 Once it has finished saving, the saving label will be hidden from the screen. 保存完成后,将在屏幕上隐藏保存标签。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM