博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用AVCaptureSession捕捉静态图片
阅读量:5250 次
发布时间:2019-06-14

本文共 4444 字,大约阅读时间需要 14 分钟。

#import 
#import
#import
@interface ViewController : UIViewController@property (strong, nonatomic) AVCaptureSession *captureSession;@property (strong, nonatomic) AVCaptureDeviceInput *videoInput;@property (strong, nonatomic) AVCaptureStillImageOutput *stillImageOutput;- (IBAction)capture:(id)sender;@end
- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    self.captureSession = [[AVCaptureSession alloc] init];    //Optional: self.captureSession.sessionPreset = AVCaptureSessionPresetMedium;        AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];        NSError * error = nil;    self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];    if (self.videoInput)    {        [self.captureSession addInput:self.videoInput];    }    else    {        NSLog(@"Input Error: %@", error);    }        self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];//AVCaptureMovieFileOutput、AVCaptureVideoDataOutput、AVCaptureAudioFileOutput、AVCaptureAudioDataOutput NSDictionary *stillImageOutputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil]; [self.stillImageOutput setOutputSettings:stillImageOutputSettings]; [self.captureSession addOutput:self.stillImageOutput]; AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession]; UIView *aView = self.view; previewLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-70); [aView.layer addSublayer:previewLayer]; }
- (IBAction)capture:(id)sender {        AVCaptureConnection *stillImageConnection = [self.stillImageOutput.connections objectAtIndex:0];    if ([stillImageConnection isVideoOrientationSupported])        [stillImageConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];        [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:stillImageConnection                                                         completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)     {         if (imageDataSampleBuffer != NULL)         {             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];             ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];             UIImage *image = [[UIImage alloc] initWithData:imageData];             [library writeImageToSavedPhotosAlbum:[image CGImage]                                       orientation:(ALAssetOrientation)[image imageOrientation]                                   completionBlock:^(NSURL *assetURL, NSError *error)                {                    UIAlertView *alert;                    if (!error)                    {                        alert = [[UIAlertView alloc] initWithTitle:@"Photo Saved"                                                           message:@"The photo was successfully saved to your photos library"                                                          delegate:nil                                                 cancelButtonTitle:@"OK"                                                 otherButtonTitles:nil, nil];                    }                    else                    {                        alert = [[UIAlertView alloc] initWithTitle:@"Error Saving Photo"                                                           message:@"The photo was not saved to your photos library"                                                          delegate:nil                                                 cancelButtonTitle:@"OK"                                                 otherButtonTitles:nil, nil];                    }                                        [alert show];                }              ];         }         else             NSLog(@"Error capturing still image: %@", error);     }]; }
- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    [self.captureSession startRunning];}- (void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];    [self.captureSession stopRunning];}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}

 

转载于:https://www.cnblogs.com/fengmin/p/5523220.html

你可能感兴趣的文章
如何在vue单页应用中使用百度地图
查看>>
Springboot使用步骤
查看>>
Spring属性注入
查看>>
Springboot-配置文件
查看>>
Springboot-日志框架
查看>>
P1192-台阶问题
查看>>
一、使用pip安装Python包
查看>>
spring与quartz整合
查看>>
Kattis之旅——Eight Queens
查看>>
3.PHP 教程_PHP 语法
查看>>
Duilib扩展《01》— 双击、右键消息扩展
查看>>
利用Fiddler拦截接口请求并篡改数据
查看>>
python习题:unittest参数化-数据从文件或excel中读取
查看>>
在工程中要加入新的错误弹出方法
查看>>
PS 滤镜— — sparkle 效果
查看>>
网站产品设计
查看>>
代理ARP
查看>>
go 学习笔记(4) ---项目结构
查看>>
java中静态代码块的用法 static用法详解
查看>>
Java线程面试题
查看>>