您好,登錄后才能下訂單哦!
小編給大家分享一下iOS如何使用視聽媒體框架AVFoundation實現照片拍攝,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
首先看一下效果圖:
下面貼上核心控制器代碼:
#import "HWPhotoVC.h" #import <AVFoundation/AVFoundation.h> @interface HWPhotoVC () @property (nonatomic, strong) AVCaptureSession *captureSession;//負責輸入和輸出設備之間的數據傳遞 @property (nonatomic, strong) AVCaptureDeviceInput *captureDeviceInput;//負責從AVCaptureDevice獲得輸入數據 @property (nonatomic, strong) AVCaptureStillImageOutput *captureStillImageOutput;//照片輸出流 @property (nonatomic, strong) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;//相機拍攝預覽圖層 @property (nonatomic, weak) UIView *containerView;//內容視圖 @property (nonatomic, weak) UIImageView *focusCursor;//聚焦按鈕 @property (nonatomic, weak) UIImageView *imgView;//拍攝照片 @end @implementation HWPhotoVC - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"拍照"; self.view.backgroundColor = [UIColor whiteColor]; //創建控件 [self creatControl]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //初始化信息 [self initPhotoInfo]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.captureSession startRunning]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [self.captureSession stopRunning]; } - (void)creatControl { CGFloat btnW = 150.f; CGFloat btnH = 40.f; CGFloat marginY = 20.f; CGFloat w = [UIScreen mainScreen].bounds.size.width; CGFloat h = [UIScreen mainScreen].bounds.size.height; //內容視圖 CGFloat containerViewH = h - 64 - btnH - marginY * 3; UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(10, 64 + marginY, w - 20, containerViewH)]; containerView.backgroundColor = [UIColor whiteColor]; containerView.layer.borderWidth = 1.f; containerView.layer.borderColor = [[UIColor grayColor] CGColor]; [self.view addSubview:containerView]; _containerView = containerView; //攝像頭切換按鈕 CGFloat cameraSwitchBtnW = 50.f; CGFloat cameraSwitchBtnMargin = 10.f; UIButton *cameraSwitchBtn = [[UIButton alloc] initWithFrame:CGRectMake(containerView.bounds.size.width - cameraSwitchBtnW - cameraSwitchBtnMargin, 64 + marginY + cameraSwitchBtnMargin, cameraSwitchBtnW, cameraSwitchBtnW)]; [cameraSwitchBtn setImage:[UIImage imageNamed:@"camera_switch"] forState:UIControlStateNormal]; [cameraSwitchBtn addTarget:self action:@selector(cameraSwitchBtnOnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:cameraSwitchBtn]; //聚焦圖片 UIImageView *focusCursor = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 75, 75)]; focusCursor.alpha = 0; focusCursor.image = [UIImage imageNamed:@"camera_focus_red"]; [containerView addSubview:focusCursor]; _focusCursor = focusCursor; //拍攝照片容器 UIImageView *imgView = [[UIImageView alloc] initWithFrame:containerView.frame]; imgView.hidden = YES; imgView.layer.borderWidth = 1.f; imgView.layer.borderColor = [[UIColor grayColor] CGColor]; imgView.contentMode = UIViewContentModeScaleAspectFill; imgView.clipsToBounds = YES; [self.view addSubview:imgView]; _imgView = imgView; //按鈕 NSArray *titleArray = @[@"拍攝照片", @"重新拍攝"]; CGFloat btnY = CGRectGetMaxY(containerView.frame) + marginY; CGFloat margin = (w - btnW * titleArray.count) / (titleArray.count + 1); for (int i = 0; i < titleArray.count; i++) { CGFloat btnX = margin + (margin + btnW) * i; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(btnX, btnY, btnW, btnH)]; btn.tag = 1000 + i; [btn setTitle:titleArray[i] forState:UIControlStateNormal]; btn.backgroundColor = [UIColor orangeColor]; btn.layer.cornerRadius = 2.0f; btn.layer.masksToBounds = YES; if (i == 1) { btn.hidden = YES; } [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } } - (void)initPhotoInfo { //初始化會話 _captureSession = [[AVCaptureSession alloc] init]; //設置分辨率 if ([_captureSession canSetSessionPreset:AVCaptureSessionPreset1280x720]) { _captureSession.sessionPreset = AVCaptureSessionPreset1280x720; } //獲得輸入設備,取得后置攝像頭 AVCaptureDevice *captureDevice = [self getCameraDeviceWithPosition:AVCaptureDevicePositionBack]; if (!captureDevice) { NSLog(@"取得后置攝像頭時出現問題"); return; } NSError *error = nil; //根據輸入設備初始化設備輸入對象,用于獲得輸入數據 _captureDeviceInput = [[AVCaptureDeviceInput alloc]initWithDevice:captureDevice error:&error]; if (error) { NSLog(@"取得設備輸入對象時出錯,錯誤原因:%@", error.localizedDescription); return; } //初始化設備輸出對象,用于獲得輸出數據 _captureStillImageOutput = [[AVCaptureStillImageOutput alloc] init]; NSDictionary *outputSettings = @{AVVideoCodecKey:AVVideoCodecJPEG}; //輸出設置 [_captureStillImageOutput setOutputSettings:outputSettings]; //將設備輸入添加到會話中 if ([_captureSession canAddInput:_captureDeviceInput]) { [_captureSession addInput:_captureDeviceInput]; } //將設備輸出添加到會話中 if ([_captureSession canAddOutput:_captureStillImageOutput]) { [_captureSession addOutput:_captureStillImageOutput]; } //創建視頻預覽層,用于實時展示攝像頭狀態 _captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession]; //攝像頭方向 AVCaptureConnection *captureConnection = [self.captureVideoPreviewLayer connection]; captureConnection.videoOrientation = AVCaptureVideoOrientationPortrait; CALayer *layer = _containerView.layer; layer.masksToBounds = YES; _captureVideoPreviewLayer.frame = layer.bounds; //填充模式 _captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; //將視頻預覽層添加到界面中 [layer insertSublayer:_captureVideoPreviewLayer below:self.focusCursor.layer]; [self addNotificationToCaptureDevice:captureDevice]; [self addGenstureRecognizer]; } - (void)btnOnClick:(UIButton *)btn { if (btn.tag == 1000) { //拍攝照片 [self photoBtnOnClick]; }else if (btn.tag == 1001) { //重新拍攝 [self resetPhoto]; } } #pragma mark 拍照 - (void)photoBtnOnClick { //根據設備輸出獲得連接 AVCaptureConnection *captureConnection = [self.captureStillImageOutput connectionWithMediaType:AVMediaTypeVideo]; captureConnection.videoOrientation = AVCaptureVideoOrientationPortrait; //根據連接取得設備輸出的數據 [self.captureStillImageOutput captureStillImageAsynchronouslyFromConnection:captureConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { if (imageDataSampleBuffer) { NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; UIImage *image = [UIImage imageWithData:imageData]; _imgView.image = image; _imgView.hidden = NO; } }]; UIButton *btn = (UIButton *)[self.view viewWithTag:1001]; btn.hidden = NO; } //重新拍攝 - (void)resetPhoto { _imgView.hidden = YES; UIButton *btn = (UIButton *)[self.view viewWithTag:1001]; btn.hidden = YES; } #pragma mark - 通知 //給輸入設備添加通知 - (void)addNotificationToCaptureDevice:(AVCaptureDevice *)captureDevice { //注意添加區域改變捕獲通知必須首先設置設備允許捕獲 [self changeDeviceProperty:^(AVCaptureDevice *captureDevice) { captureDevice.subjectAreaChangeMonitoringEnabled = YES; }]; //捕獲區域發生改變 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(areaChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:captureDevice]; } - (void)removeNotificationFromCaptureDevice:(AVCaptureDevice *)captureDevice { [[NSNotificationCenter defaultCenter] removeObserver:self name:AVCaptureDeviceSubjectAreaDidChangeNotification object:captureDevice]; } //移除所有通知 - (void)removeNotification { [[NSNotificationCenter defaultCenter] removeObserver:self]; } //設備連接成功 - (void)deviceConnected:(NSNotification *)notification { NSLog(@"設備已連接..."); } //設備連接斷開 - (void)deviceDisconnected:(NSNotification *)notification { NSLog(@"設備已斷開."); } //捕獲區域改變 - (void)areaChange:(NSNotification *)notification { NSLog(@"捕獲區域改變..."); } #pragma mark - 私有方法 //取得指定位置的攝像頭 - (AVCaptureDevice *)getCameraDeviceWithPosition:(AVCaptureDevicePosition )position { NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice *camera in cameras) { if ([camera position] == position) { return camera; } } return nil; } #pragma mark 切換前后攝像頭 - (void)cameraSwitchBtnOnClick { AVCaptureDevice *currentDevice = [self.captureDeviceInput device]; AVCaptureDevicePosition currentPosition = [currentDevice position]; [self removeNotificationFromCaptureDevice:currentDevice]; AVCaptureDevice *toChangeDevice; AVCaptureDevicePosition toChangePosition = AVCaptureDevicePositionFront; if (currentPosition == AVCaptureDevicePositionUnspecified || currentPosition == AVCaptureDevicePositionFront) { toChangePosition = AVCaptureDevicePositionBack; } toChangeDevice = [self getCameraDeviceWithPosition:toChangePosition]; [self addNotificationToCaptureDevice:toChangeDevice]; //獲得要調整的設備輸入對象 AVCaptureDeviceInput *toChangeDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:toChangeDevice error:nil]; //改變會話的配置前一定要先開啟配置,配置完成后提交配置改變 [self.captureSession beginConfiguration]; //移除原有輸入對象 [self.captureSession removeInput:self.captureDeviceInput]; //添加新的輸入對象 if ([self.captureSession canAddInput:toChangeDeviceInput]) { [self.captureSession addInput:toChangeDeviceInput]; self.captureDeviceInput = toChangeDeviceInput; } //提交會話配置 [self.captureSession commitConfiguration]; } //改變設備屬性的統一操作方法 - (void)changeDeviceProperty:(void (^)(AVCaptureDevice *))propertyChange { AVCaptureDevice *captureDevice = [self.captureDeviceInput device]; NSError *error; //注意改變設備屬性前一定要首先調用lockForConfiguration:調用完之后使用unlockForConfiguration方法解鎖 if ([captureDevice lockForConfiguration:&error]) { propertyChange(captureDevice); [captureDevice unlockForConfiguration]; }else { NSLog(@"設置設備屬性過程發生錯誤,錯誤信息:%@", error.localizedDescription); } } //設置閃光燈模式 - (void)setFlashMode:(AVCaptureFlashMode)flashMode { [self changeDeviceProperty:^(AVCaptureDevice *captureDevice) { if ([captureDevice isFlashModeSupported:flashMode]) { [captureDevice setFlashMode:flashMode]; } }]; } //設置聚焦模式 - (void)setFocusMode:(AVCaptureFocusMode)focusMode { [self changeDeviceProperty:^(AVCaptureDevice *captureDevice) { if ([captureDevice isFocusModeSupported:focusMode]) { [captureDevice setFocusMode:focusMode]; } }]; } //設置曝光模式 - (void)setExposureMode:(AVCaptureExposureMode)exposureMode { [self changeDeviceProperty:^(AVCaptureDevice *captureDevice) { if ([captureDevice isExposureModeSupported:exposureMode]) { [captureDevice setExposureMode:exposureMode]; } }]; } //設置聚焦點 - (void)focusWithMode:(AVCaptureFocusMode)focusMode exposureMode:(AVCaptureExposureMode)exposureMode atPoint:(CGPoint)point { [self changeDeviceProperty:^(AVCaptureDevice *captureDevice) { if ([captureDevice isFocusModeSupported:focusMode]) { [captureDevice setFocusMode:AVCaptureFocusModeAutoFocus]; } if ([captureDevice isFocusPointOfInterestSupported]) { [captureDevice setFocusPointOfInterest:point]; } if ([captureDevice isExposureModeSupported:exposureMode]) { [captureDevice setExposureMode:AVCaptureExposureModeAutoExpose]; } if ([captureDevice isExposurePointOfInterestSupported]) { [captureDevice setExposurePointOfInterest:point]; } }]; } //添加點按手勢,點按時聚焦 - (void)addGenstureRecognizer { [self.containerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapScreen:)]]; } - (void)tapScreen:(UITapGestureRecognizer *)tapGesture { CGPoint point = [tapGesture locationInView:self.containerView]; //將UI坐標轉化為攝像頭坐標 CGPoint cameraPoint = [self.captureVideoPreviewLayer captureDevicePointOfInterestForPoint:point]; [self setFocusCursorWithPoint:point]; [self focusWithMode:AVCaptureFocusModeAutoFocus exposureMode:AVCaptureExposureModeAutoExpose atPoint:cameraPoint]; } //設置聚焦光標位置 - (void)setFocusCursorWithPoint:(CGPoint)point { self.focusCursor.center = point; self.focusCursor.transform = CGAffineTransformMakeScale(1.5, 1.5); self.focusCursor.alpha = 1.0; [UIView animateWithDuration:1.0 animations:^{ self.focusCursor.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { self.focusCursor.alpha = 0; }]; } - (void)dealloc { [self removeNotification]; } @end
以上是“iOS如何使用視聽媒體框架AVFoundation實現照片拍攝”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。