//session
// HSZJLCameraViewC.mapp
// HSZJLCameraide
//工具
// Created by 紫霞大仙 on 16/1/16.atom
// Copyright © 2016年 Hipal. All rights reserved.code
//對象
#import "HSZJLCameraViewC.h"圖片
#import <AVFoundation/AVFoundation.h>ip
#import "HSSaveViewController.h"內存
@interface HSZJLCameraViewC ()
/**
* 相機顯示區域
*/
@property (weak, nonatomic) IBOutlet UIView *cameraView;
/**
* 水印區域
*/
@property (weak, nonatomic) IBOutlet UIView *logoView;
/**
* 工具欄
*/
@property (weak, nonatomic) IBOutlet UIView *toolView;
/**
* 設備之間的數據傳遞 第一個建立
*/
@property (nonatomic, strong)AVCaptureSession * session;
/**
* 輸入流->筆者認爲是相機
*/
@property (nonatomic, strong)AVCaptureDeviceInput * videoInput;
/**
* 照相機
*/
@property (nonatomic, strong)AVCaptureStillImageOutput * stillImageOutput;
/**
* 顯示層 (相框) 要第二個建立
*/
@property (nonatomic, strong)AVCaptureVideoPreviewLayer * previewLayer;
/**
* 水印照片
*/
@property (weak, nonatomic) IBOutlet UIImageView *logoImageVIew;
@end
@implementation HSZJLCameraViewC
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
// iOS 7
[self initSession];
}
else {
// iOS 8
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (status) {
case AVAuthorizationStatusAuthorized:
[self initSession];
break;
case AVAuthorizationStatusNotDetermined: {
// 用戶受權
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if(granted){
[self initSession];
} else {
[self dismissViewControllerAnimated:YES completion:^{
}];
}
}];
}
break;
default:
break;
}
}
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.clipsToBounds = NO;
self.view.backgroundColor = [UIColor blackColor];
[self setUpCameraLayer];
}
/**
* 初始化
*/
- (void)initSession
{
self.session = [[AVCaptureSession alloc] init];
self.session.sessionPreset = AVCaptureSessionPresetPhoto;
self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backCamera] error:nil];
self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil];
//以JPEG的圖片格式輸出圖片
[self.stillImageOutput setOutputSettings:outputSettings];
if ([self.session canAddInput:self.videoInput]) {
[self.session addInput:self.videoInput];
}
if ([self.session canAddOutput:self.stillImageOutput]) {
[self.session addOutput:self.stillImageOutput];
}
}
/**
* 設置相機layer
*/
- (void) setUpCameraLayer
{
if (self.previewLayer == nil) {
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
UIView * view = self.cameraView;
CALayer * viewLayer = [view layer];
[viewLayer setMasksToBounds:YES];
CGRect bounds = [view bounds];
bounds.size.width = [UIScreen mainScreen].bounds.size.width;
bounds.size.height = [UIScreen mainScreen].bounds.size.height * 0.6;
[self.previewLayer setFrame:bounds];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[viewLayer insertSublayer:self.previewLayer below:[[viewLayer sublayers] objectAtIndex:0]];
}
}
/**
* 拍照
*/
- (void)openCamera{
[self shutterCamera];
}
/**
* 獲取先後攝像頭對象的方法
*/
- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition) position {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if ([device position] == position) {
return device;
}
}
return nil;
}
//前置攝像頭
- (AVCaptureDevice *)frontCamera {
return [self cameraWithPosition:AVCaptureDevicePositionFront];
}
//後置攝像頭
- (AVCaptureDevice *)backCamera {
return [self cameraWithPosition:AVCaptureDevicePositionBack];
}
/**
* 先後攝像頭切換
*/
- (void)toggleCamera {
NSUInteger cameraCount = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count];
if (cameraCount > 1) {
NSError *error;
AVCaptureDeviceInput *newVideoInput;
AVCaptureDevicePosition position = [[_videoInput device] position];
if (position == AVCaptureDevicePositionBack)
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self frontCamera] error:&error];
else if (position == AVCaptureDevicePositionFront)
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backCamera] error:&error];
else
return;
if (newVideoInput != nil) {
[self.session beginConfiguration];
[self.session removeInput:self.videoInput];
if ([self.session canAddInput:newVideoInput]) {
[self.session addInput:newVideoInput];
[self setVideoInput:newVideoInput];
} else {
[self.session addInput:self.videoInput];
}
[self.session commitConfiguration];
} else if (error) {
NSLog(@"打開失敗 %@", error);
}
}
}
#pragma mark -- 拍照
- (void) shutterCamera
{
AVCaptureConnection * videoConnection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
if (!videoConnection) {
NSLog(@"拍照失敗");
return;
}
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer == NULL) {
return;
}
//照片
NSData * imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage * image = [UIImage imageWithData:imageData];
HSSaveViewController *saVC = [[HSSaveViewController alloc] initWithNibName:@"HSSaveViewController" bundle:nil];
UIImage *logo = [UIImage imageNamed:@"picture_logo1"];
[self presentViewController:saVC animated:YES completion:^{
[saVC setPhotoImage:image logoImage:logo];
}];
}];
}
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
UIGraphicsBeginImageContext(image1.size);
// Draw image1
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
// Draw image2
[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
#pragma mark --加水印code
- (UIImage *)completeEditWithImage:(UIImage*)image {
CGSize size = image.size;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width-2, size.height-2), NO, 1.0);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *logo = [UIImage imageNamed:@"picture_logo.png"];
[logo drawAtPoint:CGPointMake(100, 100)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
/**
* 由於在一切低端機 會莫名其妙的內存警告 startRunning -> 映射到屏幕上
*/
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
if (self.session) {
[self.session startRunning];
}
}
- (void) viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear: animated];
if (self.session) {
[self.session stopRunning];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
NSLog(@"相機內存警告");
}
- (IBAction)takePictureClick:(id)sender {
[self shutterCamera];
}
- (IBAction)cancelClick:(UIButton *)sender {
if (self.navigationController.viewControllers.count >1) {
[self.navigationController popViewControllerAnimated:YES];
}else{
[self dismissViewControllerAnimated:YES completion:^{
}];
}
}
- (IBAction)changeCameraClick:(UIButton *)sender {
[self toggleCamera];
}
- (IBAction)choseLogo:(UIButton*)sender {
switch (sender.tag) {
case 0:
{
//不選
self.logoImageVIew.image = nil;
}
break;
case 1:
{
self.logoImageVIew.image = [UIImage imageNamed:@"picture_logo"];
}
break;
case 2:
{
self.logoImageVIew.image = [UIImage imageNamed:@"picture_logo1"];
}
break;
case 3:
{
self.logoImageVIew.image = [UIImage imageNamed:@"picture_logo"];
}
break;
default:
break;
}
}
@end