gpt4 book ai didi

ios - 如何使用 AVCaptureDevice 保存图像

转载 作者:行者123 更新时间:2023-11-29 13:05:25 25 4
gpt4 key购买 nike

我在我的项目中使用了 Apple 的 GLCameraRipple 示例代码,我想知道是否有办法拍摄照片/视频?项目可以在 https://developer.apple.com/library/ios/samplecode/GLCameraRipple/Introduction/Intro.html 找到

最佳答案

首先,确保您已包含 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];
}

还需要实现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 函数,上述函数将在屏幕上显示“Saving..”标签。它只是意味着它当前正在将视频输入保存为图像并将其存储到照片库中。保存完成后,保存标签将从屏幕上隐藏。希望这对您有所帮助!

关于ios - 如何使用 AVCaptureDevice 保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18760725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com