如何使用AVCaptureDevice保存图像

时间:2021-10-15 21:22:32

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? Project can be found at https://developer.apple.com/library/ios/samplecode/GLCameraRipple/Introduction/Intro.html

我在我的项目中使用了苹果的GLCameraRipple示例代码,我想知道是否有方法可以使用pic/video?项目可以在https://developer.apple.com/library/ios/samplecode/glcameraripple/tion/intro.html找到

1 个解决方案

#1


1  

First of all, ensure that you have included AssetLibrary framework, because we need that to access the photo library. 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.

首先,确保包含了AssetLibrary框架,因为我们需要它来访问照片库。假设您已经设置了捕获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. One example is as follows:

您还需要实现图像:didFinishSavingWithError:contextInfo:方法作为保存图像的完成函数。一个例子如下:

-(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. 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!

当您触发captureMultipleTimes函数时,上面的函数将在屏幕上显示“save ..”标签。它只是表示当前正在将视频输入保存为图像,并将其存储到photo lib中,保存完成后,保存标签将从屏幕中隐藏。希望这可以帮助!

#1


1  

First of all, ensure that you have included AssetLibrary framework, because we need that to access the photo library. 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.

首先,确保包含了AssetLibrary框架,因为我们需要它来访问照片库。假设您已经设置了捕获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. One example is as follows:

您还需要实现图像:didFinishSavingWithError:contextInfo:方法作为保存图像的完成函数。一个例子如下:

-(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. 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!

当您触发captureMultipleTimes函数时,上面的函数将在屏幕上显示“save ..”标签。它只是表示当前正在将视频输入保存为图像,并将其存储到photo lib中,保存完成后,保存标签将从屏幕中隐藏。希望这可以帮助!