iOS自定义相机

2017-10-23  本文已影响0人  守护浪漫的小香樟

最近不忙,有小伙伴儿让帮忙写个手动(按音量键)自动拍照  并上传照片,想着自己没有这方面的需求 就当学知识了 

一、如图:创建相机属性

设定了拍照成功之后的代理方法(因为上传和拍照是分开的)拍照的张数  时间   以及拍照模式等属性

二、创建拍照环境

//搭建设备环境

self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];

self.captureOutput = [[AVCaptureStillImageOutput alloc] init];

// ZLCameraView     初始化相机视图

ZLCameraView * caramView = [[ZLCameraView alloc] initWithFrame:CGRectMake(0, 40, viewWidth, viewHeight)];

caramView.backgroundColor = [UIColor clearColor];

caramView.delegate = self;

[self.view addSubview:caramView];

三、创建计时自动拍照   和手动按音量键监听(在使用此方法时候要注意:1、调用时候判断拍照模式;2、中途取消要关闭线程)

/**

*  添加计时线程   

*/

- (void)manageCountDown:(int)count{

timeOut = count;

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

_countTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);

dispatch_source_set_timer(_countTimer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);

dispatch_source_set_event_handler(_countTimer, ^{

if(timeOut <= 0) {

dispatch_source_cancel(_countTimer);

dispatch_async(dispatch_get_main_queue(), ^{

//[self takePhotosCancel];

});

}else {

timeOut--;

dispatch_async(dispatch_get_main_queue(), ^{

int X = count - timeOut;

int F = X % self.everyTime;

if (F == 0) {

NSLog(@"拍照 ");

[self stillImageMotherd:nil];

}

});

}

});

dispatch_resume(_countTimer);

}

#pragma mark 监听点击音量建  注册

-(void)initAudioSession{

NSError * error;

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

[[AVAudioSession sharedInstance] setActive:YES error:&error];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];

}

#pragma mark 监听点击音量建

- (void)volumeChanged:(NSNotification *)notification

{

NSLog(@"点击了音量键");

[self stillImageMotherd:nil];

}

四、最关键的  调用拍照方法,并获取拍照之后的image   把他座位代理方法的参数传回去上级页面(进行上传之类的操作)

#pragma mark  拍照出zhaopiande方法

-(void)Captureimage

{

AVCaptureConnection * videoConnection = nil;

for (AVCaptureConnection *connection in self.captureOutput.connections) {

for (AVCaptureInputPort *port in [connection inputPorts]) {

if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {

videoConnection = connection;

break;

}

}

if (videoConnection) {

break;

}

}

__weak typeof(self) WeakSelf = self;

[self.captureOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:

^(CMSampleBufferRef imageSampleBuffer, NSError *error) {

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];

UIImage *t_image = [UIImage imageWithData:imageData];

t_image = [WeakSelf cutImage:t_image];

t_image = [WeakSelf fixOrientation:t_image];

NSLog(@"拍照结束:%@",t_image);

if (WeakSelf.cameraType == ZLCameraSingle) {

[WeakSelf.images removeAllObjects];

[WeakSelf.images addObject:t_image];

} else{

[WeakSelf.images addObject:t_image];

}

[WeakSelf.delegete takePhotos:WeakSelf.images];

if (WeakSelf.images.count == WeakSelf.count && !self.IsHandStyle) {

[WeakSelf dismissViewControllerAnimated:NO completion:nil];

}

[_Cameraview doneAction];

}];

}

五、最后获得了image   遵守协议上传给服务器(在第四步对图片进行了压缩处理)

//利用获得的图片和服务器URL上传(由于服务器没有优化完成,相关的加密没有添加,需要的话可以进行添加)

-(void)uploadPhotosWithImage:(UIImage *)image  withUrl:(NSString *)url;

AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];    manager.responseSerializer = [AFHTTPResponseSerializer serializer];    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];    manager.requestSerializer.timeoutInterval = 10.f;    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];        [manager POST:url parameters:@{} constructingBodyWithBlock:^(id_Nonnull formData) {

} progress:^(NSProgress * _Nonnull uploadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject){

self.uploadLabel.text = @"上传成功";} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

NSLog(@"上传失败 %@", error);}];

附件:Demo地址:https://github.com/JamesBondMine/Camera-Photos

相关截图

上一篇下一篇

猜你喜欢

热点阅读