Ios 如何使用AVCaptureDevice保存图像

Ios 如何使用AVCaptureDevice保存图像,ios,objective-c,avcapturesession,avcapture,avcapturedevice,Ios,Objective C,Avcapturesession,Avcapture,Avcapturedevice,我正在我的项目中使用苹果的GLCameraRipple示例代码,我想知道是否有一种方法可以拍摄照片/视频?项目可在中找到。首先,请确保已包含AssetLibrary框架,因为我们需要它来访问照片库。假设您已经正确设置了capture AVCaptureSession、AVCaptureDevice和AVCaptureStillImageOutput(stillImage),现在您可以创建一个按钮或简单地调用下面的函数来保存图像 -(void)captureMultipleTimes { AVCa

我正在我的项目中使用苹果的GLCameraRipple示例代码,我想知道是否有一种方法可以拍摄照片/视频?项目可在

中找到。首先,请确保已包含AssetLibrary框架,因为我们需要它来访问照片库。假设您已经正确设置了capture 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];
}
您还需要实现image: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];
}
}

一旦您触发captureMultipleTimes功能,上述功能将在屏幕上显示“Saveing..”标签。这仅仅意味着它当前正在将视频输入保存为图像,并将其存储到照片库中。保存完成后,保存标签将从屏幕上隐藏。希望这有帮助

首先,确保包含AssetLibrary框架,因为我们需要它来访问照片库。假设您已经正确设置了capture 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];
}
您还需要实现image: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];
}
}

一旦您触发captureMultipleTimes功能,上述功能将在屏幕上显示“Saveing..”标签。这仅仅意味着它当前正在将视频输入保存为图像,并将其存储到照片库中。保存完成后,保存标签将从屏幕上隐藏。希望这有帮助

选中此链接您需要在设备上运行代码才能访问这些,它不会发生在模拟器上选中此链接您需要在设备上运行代码才能访问这些,它不会发生在模拟器上