码迷,mamicode.com
首页 > 其他好文 > 详细

使用AVCaptureSession捕捉静态图片

时间:2016-05-24 15:13:32      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>

@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);
}

 

使用AVCaptureSession捕捉静态图片

标签:

原文地址:http://www.cnblogs.com/fengmin/p/5523220.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!