// // CameraManager.m // PanoramicCameraSDK // // Created by 王昭威 on 2023/1/14. // #import "CameraManager.h" @interface CameraManager () @property (nonatomic, assign) CGFloat backAspect; @property (nonatomic, assign) CGFloat frontAspect; @end @implementation CameraManager @synthesize front = _front; @synthesize back = _back; - (void)setResolutionFor:(AVCaptureDevicePosition)position with: (NSInteger)desiredWidth{ AVCaptureDevice* device = [self cameraWithPosition:position]; if(device == nil){ return; } NSArray *supportedFormats = device.formats; AVCaptureDeviceFormat *bestFormat; CGFloat aspect = 1; for (AVCaptureDeviceFormat *format in supportedFormats) { CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[format formatDescription]); //分辨率 if (dimensions.width <= desiredWidth) { if(dimensions.height > 0) aspect = (CGFloat)dimensions.width / dimensions.height; bestFormat = format; } } [device lockForConfiguration:nil]; [device setActiveFormat:bestFormat]; [device setActiveVideoMaxFrameDuration:CMTimeMake(1, 10)]; [device setActiveVideoMinFrameDuration:CMTimeMake(1, 10)]; [device unlockForConfiguration]; if(device.position == AVCaptureDevicePositionFront){ self.frontAspect = aspect; } else if(device.position == AVCaptureDevicePositionBack){ self.backAspect = aspect; } } - (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position { NSArray *devices = nil; if(@available(iOS 10, *)){ AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position:position]; devices = [captureDeviceDiscoverySession devices]; } else if(@available(iOS 4, *)){ devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; } if(devices == nil){ return nil; } for (AVCaptureDevice *device in devices) { if ([device position] == position) return device; } return nil; } + (instancetype)shared{ static dispatch_once_t token; static CameraManager* instance = nil; dispatch_once(&token, ^{ instance = [[self alloc] init]; }); return instance; } - (instancetype)init{ self = [super init]; if(self){ _back = [self cameraWithPosition:AVCaptureDevicePositionBack]; _front = [self cameraWithPosition:AVCaptureDevicePositionFront]; _frontAspect = 1; _backAspect = 1; } return self; } - (void)focusOn:(CGPoint)point{ if (self.back == nil) { return; } [self focusOn:point device:self.back]; } - (void)focusOn:(CGPoint)point device:(AVCaptureDevice *)device{ NSError* error; [device lockForConfiguration:&error]; if(error != nil){ return; } if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]){ [device setFocusPointOfInterest: point]; device.focusMode = AVCaptureFocusModeContinuousAutoFocus; } if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) { [device setExposurePointOfInterest:point]; device.exposureMode = AVCaptureExposureModeContinuousAutoExposure; } [device unlockForConfiguration]; } @end