自定义相机功能

该代码实现了一个简单的iOS相机界面,包括拍照、切换前后摄像头、焦点控制和闪光灯功能。利用AVFoundation框架,创建AVCaptureSession来捕获图像,设置自动白平衡和自动对焦,并提供了预览层来实时显示相机画面。用户可以点击屏幕进行对焦,同时支持手动切换摄像头和开启/关闭闪光灯。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于项目一个需求,简单写了下,还没写好说不需要了……

//
//  
//  
//
//  Created by 方知友 on 11.3.22.
//

#import "WGTakePhotoViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface WGTakePhotoViewController ()<AVCapturePhotoCaptureDelegate,UIImagePickerControllerDelegate>
//捕获设备,通常是前置摄像头,后置摄像头,麦克风(音频输入)
@property(nonatomic)AVCaptureDevice *device;
//AVCaptureDeviceInput 代表输入设备
@property(nonatomic)AVCaptureDeviceInput *input;
//照片输出流
@property (nonatomic)AVCapturePhotoOutput *ImageOutPut;
//session:由他把输入输出结合在一起,并开始启动捕获设备(摄像头)
@property(nonatomic)AVCaptureSession *session;
//图像预览层,实时显示捕获的图像
@property(nonatomic)AVCaptureVideoPreviewLayer *previewLayer;
//聚焦
@property (nonatomic)UIView *focusView;
@property (nonatomic, strong) UIButton * flashBtn;
//是否开启闪光灯
@property (nonatomic)BOOL isflashOn;
@property (nonatomic, strong) UIView *backgroundView;
/// 返回键
@property (nonatomic, strong) UIButton *backBtn;
/// 闪光灯按钮
@property (nonatomic, strong) UIButton *takePhotoBtn;
/// 相册按钮
@property (nonatomic, strong) UIButton *photoLibraryBtn;
///切换前后摄像头按钮
@property (nonatomic, strong) UIButton * checkBtn;

@end

@implementation WGTakePhotoViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.backgroundView.hidden = NO;
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.backgroundView.hidden = YES;
}

- (void)dealloc
{
    [self.backgroundView removeFromSuperview];
    self.backgroundView = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = UIColor.blackColor;
    [self customCamera];
    [appWindow addSubview:self.backgroundView];
}
- (void)customCamera
{
    //使用AVMediaTypeVideo 指明self.device代表视频,默认使用后置摄像头进行初始化
    //  self.device =   [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //使用前置摄像头
    self.device = [self cameraWithPosition:AVCaptureDevicePositionFront];
    
    //使用设备初始化输入
    self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    
  //照片输出
    self.ImageOutPut = [[AVCapturePhotoOutput alloc]init];
    NSDictionary *outputformat = @{AVVideoCodecKey:AVVideoCodecJPEG};
    AVCapturePhotoSettings *outputSettings = [AVCapturePhotoSettings photoSettingsWithFormat:outputformat];
    //自动闪光
//    [outputSettings setFlashMode:AVCaptureFlashModeAuto];
    [self.ImageOutPut setPhotoSettingsForSceneMonitoring:outputSettings];
 
    //生成会话,用来结合输入输出
    self.session = [[AVCaptureSession alloc]init];
    //直接设置质量最高 不指定,免得有些旧设备崩溃
    if ([self.session canSetSessionPreset:AVCaptureSessionPresetHigh]) {
        
        [self.session setSessionPreset:AVCaptureSessionPresetHigh];
        
    }
    
    if ([self.session canAddInput:self.input]) {
        [self.session addInput:self.input];
        
    }
   
      //将设备输出添加到会话中
    if ([self.session canAddOutput:_ImageOutPut]) {
        [self.session addOutput:_ImageOutPut];
    }
 
    //使用self.session,初始化预览层,self.session负责驱动input进行信息的采集,layer负责把图像渲染显示
    self.previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.session];
    self.previewLayer.frame = CGRectMake(0, zNavigationHeight, zScreenWidth, zScreenHeight-2*zNavigationHeight);
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:self.previewLayer];
    
    //开始启动
    [self.session startRunning];
    
    //修改设备的属性,先加锁
    if ([self.device lockForConfiguration:nil]) {
     
        //自动白平衡
        if ([self.device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeAutoWhiteBalance]) {
            [self.device setWhiteBalanceMode:AVCaptureWhiteBalanceModeAutoWhiteBalance];
        }
        
        //解锁
        [self.device unlockForConfiguration];
    }
    
}

- (void)focusGesture:(UITapGestureRecognizer*)gesture{
   CGPoint point = [gesture locationInView:gesture.view];
   [self focusAtPoint:point];
}
- (void)focusAtPoint:(CGPoint)point{
   CGSize size = self.view.bounds.size;
   // focusPoint 函数后面Point取值范围是取景框左上角(0,0)到取景框右下角(1,1)之间,按这个来但位置就是不对,只能按上面的写法才可以。前面是点击位置的y/PreviewLayer的高度,后面是1-点击位置的x/PreviewLayer的宽度
   CGPoint focusPoint = CGPointMake( point.y /size.height ,1 - point.x/size.width );
   
   if ([self.device lockForConfiguration:nil]) {
       
       if ([self.device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
           [self.device setFocusPointOfInterest:focusPoint];
           [self.device setFocusMode:AVCaptureFocusModeAutoFocus];
       }
       
       if ([self.device isExposureModeSupported:AVCaptureExposureModeAutoExpose ]) {
           [self.device setExposurePointOfInterest:focusPoint];
           //曝光量调节
           [self.device setExposureMode:AVCaptureExposureModeAutoExpose];
       }
       
       [self.device unlockForConfiguration];
       _focusView.center = point;
       _focusView.hidden = NO;
       [UIView animateWithDuration:0.3 animations:^{
           self->_focusView.transform = CGAffineTransformMakeScale(1.25, 1.25);
       }completion:^(BOOL finished) {
           [UIView animateWithDuration:0.5 animations:^{
               self->_focusView.transform = CGAffineTransformIdentity;
           } completion:^(BOOL finished) {
               self->      _focusView.hidden = YES;
           }];
       }];
   }
   
}
#pragma mark -  切换摄像头
- (void)changeCamera{
    //获取摄像头的数量
    NSUInteger cameraCount = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count];
    //摄像头小于等于1的时候直接返回
    if (cameraCount <= 1) return;
    
    AVCaptureDevice *newCamera = nil;
    AVCaptureDeviceInput *newInput = nil;
    //获取当前相机的方向(前还是后)
    AVCaptureDevicePosition position = [[self.input device] position];
    
    //为摄像头的转换加转场动画
    CATransition *animation = [CATransition animation];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.duration = 0.5;
    animation.type = @"oglFlip";
    
    if (position == AVCaptureDevicePositionFront) {
        //获取后置摄像头
        newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];
        animation.subtype = kCATransitionFromLeft;
    }else{
        //获取前置摄像头
        newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];
        animation.subtype = kCATransitionFromRight;
    }
    
    [self.previewLayer addAnimation:animation forKey:nil];
    //输入流
    newInput = [AVCaptureDeviceInput deviceInputWithDevice:newCamera error:nil];
    if (newInput != nil) {
        
        [self.session beginConfiguration];
        //先移除原来的input
        [self.session removeInput:self.input];
        
        if ([self.session canAddInput:newInput]) {
            [self.session addInput:newInput];
            self.input = newInput;
            
        } else {
            //如果不能加现在的input,就加原来的input
            [self.session addInput:self.input];
        }
        
        [self.session commitConfiguration];
        
    }
}
- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position{
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for ( AVCaptureDevice *device in devices )
        if ( device.position == position ) return device;
    return nil;
}

#pragma mark- 拍照
- (void)shutterCamera
{
    AVCaptureConnection * videoConnection = [self.ImageOutPut connectionWithMediaType:AVMediaTypeVideo];
    if (videoConnection ==  nil) {
        return;
    }
    NSDictionary *outputformat = @{AVVideoCodecKey:AVVideoCodecJPEG};
    AVCapturePhotoSettings *outputSettings = [AVCapturePhotoSettings photoSettingsWithFormat:outputformat];
    [self.ImageOutPut capturePhotoWithSettings:outputSettings delegate:self];
}
#pragma mark -  AVCapturePhotoCaptureDelegate
- (void)captureOutput:(AVCapturePhotoOutput *)captureOutput didFinishProcessingPhotoSampleBuffer:(nullable CMSampleBufferRef)photoSampleBuffer previewPhotoSampleBuffer:(nullable CMSampleBufferRef)previewPhotoSampleBuffer resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings bracketSettings:(nullable AVCaptureBracketedStillImageSettings *)bracketSettings error:(nullable NSError *)error {
    
    NSData *data = [AVCapturePhotoOutput JPEGPhotoDataRepresentationForJPEGSampleBuffer:photoSampleBuffer previewPhotoSampleBuffer:previewPhotoSampleBuffer];
    UIImage *image = [UIImage imageWithData:data];
    
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
/**
 * 保存图片到相册
 */
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    NSLog(@"");
}
#pragma mark - Actions
- (void)backLastPage {
    [self.backgroundView removeFromSuperview];
    self.backgroundView = nil;
    
    [self.navigationController popViewControllerAnimated:YES];
}

/// 打开闪光灯
- (void)openOrCloseFlash {
        
    if ([_device lockForConfiguration:nil]) {
        if (_isflashOn) {
            if ([_device isFlashModeSupported:AVCaptureFlashModeOff]) {
                [_device setFlashMode:AVCaptureFlashModeOff];
                _isflashOn = NO;
            }
        }else{
            if ([_device isFlashModeSupported:AVCaptureFlashModeOn]) {
                [_device setFlashMode:AVCaptureFlashModeOn];
                _isflashOn = YES;
            }
        }
        
        [_device unlockForConfiguration];
    }
}
///展示乘车码
- (void)showTakeCarCode{

}
/// 打开本地相册
- (void)photoLibraryBtnClick {
    [self openLocalPhoto:YES];
}
- (void)openLocalPhoto:(BOOL)allowsEditing {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    picker.delegate = self;
   
    //部分机型有问题
    picker.allowsEditing = allowsEditing;
    
    
    [self presentViewController:picker animated:YES completion:nil];
    
}
//当选择一张图片后进入这里

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    __block UIImage* image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    if (!image){
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }

}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    NSLog(@"cancel");
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (UIButton *)flashBtn {
    if (!_flashBtn) {
        _flashBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_flashBtn setTitle:@"⚡️" forState:UIControlStateNormal];
        [_flashBtn setTitle:@"🌩️" forState:UIControlStateSelected];
        
        [_flashBtn addTarget:self action:@selector(openOrCloseFlash) forControlEvents:UIControlEventTouchUpInside];
    }
    return _flashBtn;
}
- (UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setBackgroundImage:[UIImage imageNamed:@"com_icon_return_white"] forState:UIControlStateNormal];
        
        [_backBtn addTarget:self action:@selector(backLastPage) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}
- (UIButton *)checkBtn {
    if (!_checkBtn) {
        _checkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_checkBtn setTitle:@"❌" forState:UIControlStateNormal];;
        
        _checkBtn.layer.cornerRadius = kNewSize(22.5);
        
        [_checkBtn addTarget:self action:@selector(changeCamera) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _checkBtn;
}
- (UIButton *)takePhotoBtn {
    if (!_takePhotoBtn) {
        _takePhotoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_takePhotoBtn setTitle:@"📷" forState:UIControlStateNormal];
        
        _takePhotoBtn.layer.cornerRadius = kNewSize(22.5);
        
        [_takePhotoBtn addTarget:self action:@selector(shutterCamera) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _takePhotoBtn;
}

- (UIButton *)photoLibraryBtn {
    if (!_photoLibraryBtn) {
        _photoLibraryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_photoLibraryBtn setTitle:@"🎑" forState:UIControlStateNormal];
        _photoLibraryBtn.layer.cornerRadius = kNewSize(22.5);
        
        [_photoLibraryBtn addTarget:self action:@selector(photoLibraryBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _photoLibraryBtn;
}

- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, zScreenWidth, zScreenHeight)];
        
        [self.backgroundView addSubview:self.checkBtn];
        self.checkBtn.frame =  CGRectMake(zScreenWidth - kNewSize(30 + 45), zScreenHeight - safeAreaBottomH - kNewSize(36 + 45), kNewSize(45), kNewSize(45));
        
        [self.backgroundView addSubview:self.takePhotoBtn];
        self.takePhotoBtn.frame = CGRectMake((zScreenWidth-kNewSize(45))*0.5, zScreenHeight - safeAreaBottomH - kNewSize(36 + 45), kNewSize(45), kNewSize(45));
        
        [self.backgroundView addSubview:self.photoLibraryBtn];
        self.photoLibraryBtn.frame = CGRectMake(kNewSize(30), zScreenHeight - safeAreaBottomH - kNewSize(36 + 45), kNewSize(45), kNewSize(45));
        [self.backgroundView addSubview:self.flashBtn];
        self.flashBtn.frame = CGRectMake(kNewSize(15), kNewSize(12) + statusBarH, kNewSize(17), kNewSize(17));
        [self.backgroundView addSubview:self.focusView];
        [_backgroundView addGestureRecognizer:[UITapGestureRecognizer gestureWithTarget:self action:@selector(focusGesture:)]];
    }
    return _backgroundView;
}
-(UIView *)focusView{
    if (!_focusView) {
        _focusView = [[UIView alloc] initWithFrame:CGRectMake(0.5*zScreenWidth-40, 0.5*zScreenHeight-40, 80, 80)];
        _focusView.backgroundColor = UIColor.clearColor;
        _focusView.layer.borderColor = UIColor.yellowColor.CGColor;
        _focusView.layer.borderWidth = 1;
        _focusView.hidden = YES;
    }
    return _focusView;
}
@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值