最近不忙,有小伙伴兒讓幫忙寫個手動(按音量鍵)自動拍照 ?并上傳照片,想著自己沒有這方面的需求 就當學知識了?
一、如圖:創建相機屬性
設定了拍照成功之后的代理方法(因為上傳和拍照是分開的)拍照的張數 ?時間 ? 以及拍照模式等屬性
二、創建拍照環境
//搭建設備環境
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
相關截圖