diff --git a/src/ios/classes/AVCamViewController.h b/src/ios/classes/AVCamViewController.h old mode 100644 new mode 100755 index 0ad4aaf..94b42dc --- a/src/ios/classes/AVCamViewController.h +++ b/src/ios/classes/AVCamViewController.h @@ -3,28 +3,29 @@ @interface AVCamViewController : UIViewController { - NSData *_imageData; - void(^_callback)(UIImage*); + NSData *_imageData; + void (^_callback)(UIImage *, NSString *, NSString *); - UIPinchGestureRecognizer *twoFingerPinch; + UIPinchGestureRecognizer *twoFingerPinch; - CGRect frameBtnThumb; - - UIImage *capturedImage; - NSData *capturedImageData; + CGRect frameBtnThumb; - BOOL isRotated; + UIImage *capturedImage; + NSData *capturedImageData; + + BOOL isRotated; CGFloat fDist; - } @property (weak, nonatomic) IBOutlet UIView *saveBgPanel; @property (weak, nonatomic) IBOutlet UIView *topBgPanel; @property (weak, nonatomic) IBOutlet UIImageView *capturedImageView; +@property (weak, nonatomic) IBOutlet UIButton *btnBigDeletePicture; @property (weak, nonatomic) IBOutlet UIButton *btnDeletePicture; @property (weak, nonatomic) IBOutlet UIButton *btnSaveImage; +@property (weak, nonatomic) IBOutlet UIButton *btnBigSaveImage; @property (weak, nonatomic) IBOutlet UISlider *opacitySlider; @@ -33,8 +34,7 @@ - -- (id) initWithParams:(CameraParameter *)parameter WithCallback:(void(^)(UIImage*))callback; +- (id)initWithParams:(CameraParameter *)parameter WithCallback:(void (^)(UIImage *, NSString *, NSString *))callback; diff --git a/src/ios/classes/AVCamViewController.m b/src/ios/classes/AVCamViewController.m old mode 100644 new mode 100755 index 50961d6..7f4549d --- a/src/ios/classes/AVCamViewController.m +++ b/src/ios/classes/AVCamViewController.m @@ -5,9 +5,9 @@ #import "AVCamPreviewView.h" -static void * CapturingStillImageContext = &CapturingStillImageContext; -static void * RecordingContext = &RecordingContext; -static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDeviceAuthorizedContext; +static void *CapturingStillImageContext = &CapturingStillImageContext; +static void *RecordingContext = &RecordingContext; +static void *SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDeviceAuthorizedContext; @interface AVCamViewController () @@ -56,30 +56,25 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic @implementation AVCamViewController @synthesize params; -- (BOOL)isSessionRunningAndDeviceAuthorized -{ - return [[self session] isRunning] && [self isDeviceAuthorized]; - - +- (BOOL)isSessionRunningAndDeviceAuthorized { + return [[self session] isRunning] && [self isDeviceAuthorized]; } -+ (NSSet *)keyPathsForValuesAffectingSessionRunningAndDeviceAuthorized -{ - return [NSSet setWithObjects:@"session.running", @"deviceAuthorized", nil]; ++ (NSSet *)keyPathsForValuesAffectingSessionRunningAndDeviceAuthorized { + return [NSSet setWithObjects:@"session.running", @"deviceAuthorized", nil]; } - - -- (id) initWithParams:(CameraParameter *)parameter WithCallback:(void(^)(UIImage*))callback { +- (id)initWithParams:(CameraParameter *)parameter WithCallback:(void (^)(UIImage *, NSString *, NSString *))callback { self = [super initWithNibName:nil bundle:nil]; self.params = parameter; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { - self = [super initWithNibName:@"AVCamViewController_iPad" bundle:nil]; - } else { - self = [super initWithNibName:@"AVCamViewController_iPhone" bundle:nil]; + self = [super initWithNibName:@"AVCamViewController_iPad" bundle:nil]; } - + else { + self = [super initWithNibName:@"AVCamViewController_iPhone" bundle:nil]; + } + if (self) { _callback = callback; @@ -87,302 +82,260 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic return self; } - -- (void)viewDidLoad -{ - [super viewDidLoad]; +- (void)viewDidLoad { + [super viewDidLoad]; - - // Create the AVCaptureSession - AVCaptureSession *session = [[AVCaptureSession alloc] init]; - [self setSession:session]; - - // Setup the preview view - [[self previewView] setSession:session]; - - // Check for device authorization - [self checkDeviceAuthorizationStatus]; - - // In general it is not safe to mutate an AVCaptureSession or any of its inputs, outputs, or connections from multiple threads at the same time. - // Why not do all of this on the main queue? - // -[AVCaptureSession startRunning] is a blocking call which can take a long time. We dispatch session setup to the sessionQueue so that the main queue isn't blocked (which keeps the UI responsive). - - dispatch_queue_t sessionQueue = dispatch_queue_create("session queue", DISPATCH_QUEUE_SERIAL); - [self setSessionQueue:sessionQueue]; - - dispatch_async(sessionQueue, ^{ - [self setBackgroundRecordingID:UIBackgroundTaskInvalid]; - - NSError *error = nil; + + // Create the AVCaptureSession + AVCaptureSession *session = [[AVCaptureSession alloc] init]; + [self setSession:session]; + + // Setup the preview view + [[self previewView] setSession:session]; + + // Check for device authorization + [self checkDeviceAuthorizationStatus]; + + // In general it is not safe to mutate an AVCaptureSession or any of its inputs, outputs, or connections from multiple threads at the same time. + // Why not do all of this on the main queue? + // -[AVCaptureSession startRunning] is a blocking call which can take a long time. We dispatch session setup to the sessionQueue so that the main queue isn't blocked (which keeps the UI responsive). + + dispatch_queue_t sessionQueue = dispatch_queue_create("session queue", DISPATCH_QUEUE_SERIAL); + [self setSessionQueue:sessionQueue]; + + dispatch_async(sessionQueue, ^{ + [self setBackgroundRecordingID:UIBackgroundTaskInvalid]; - AVCaptureDevice *videoDevice; - if(params.nDefaultCamera == 0) - { + NSError *error = nil; + + AVCaptureDevice *videoDevice; + if (params.nDefaultCamera == 0) { videoDevice = [AVCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack]; } - else{ + else { videoDevice = [AVCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionFront]; } AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error]; dispatch_async(dispatch_get_main_queue(), ^{ - if(![videoDevice hasTorch]) - { + if (![videoDevice hasTorch]) { self.btnFlash.hidden = YES; -// self.cameraButton.center = self.btnThumb.center; -// self.btnThumb.center = self.btnFlash.center; + // self.cameraButton.center = self.btnThumb.center; + // self.btnThumb.center = self.btnFlash.center; } }); - - - if (error) - { - NSLog(@"%@", error); - } - - if ([session canAddInput:videoDeviceInput]) - { - [session addInput:videoDeviceInput]; - [self setVideoDeviceInput:videoDeviceInput]; - - dispatch_async(dispatch_get_main_queue(), ^{ - // Why are we dispatching this to the main queue? - // Because AVCaptureVideoPreviewLayer is the backing layer for AVCamPreviewView and UIView can only be manipulated on main thread. - // Note: As an exception to the above rule, it is not necessary to serialize video orientation changes on the AVCaptureVideoPreviewLayer’s connection with other session manipulation. - - [[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] setVideoOrientation:(AVCaptureVideoOrientation)[self interfaceOrientation]]; - }); - } - - AVCaptureDevice *audioDevice = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] firstObject]; - AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error]; - - if (error) - { - NSLog(@"%@", error); - } - - if ([session canAddInput:audioDeviceInput]) - { - [session addInput:audioDeviceInput]; - } - - AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; - if ([session canAddOutput:movieFileOutput]) - { - [session addOutput:movieFileOutput]; - AVCaptureConnection *connection = [movieFileOutput connectionWithMediaType:AVMediaTypeVideo]; - if ([connection isVideoStabilizationSupported]) - [connection setEnablesVideoStabilizationWhenAvailable:YES]; - [self setMovieFileOutput:movieFileOutput]; - } - - AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; - if ([session canAddOutput:stillImageOutput]) - { - [stillImageOutput setOutputSettings:@{AVVideoCodecKey : AVVideoCodecJPEG}]; - [session addOutput:stillImageOutput]; - [self setStillImageOutput:stillImageOutput]; - } - }); + if (error) { + NSLog(@"%@", error); + } + + if ([session canAddInput:videoDeviceInput]) { + [session addInput:videoDeviceInput]; + [self setVideoDeviceInput:videoDeviceInput]; + + dispatch_async(dispatch_get_main_queue(), ^{ + // Why are we dispatching this to the main queue? + // Because AVCaptureVideoPreviewLayer is the backing layer for AVCamPreviewView and UIView can only be manipulated on main thread. + // Note: As an exception to the above rule, it is not necessary to serialize video orientation changes on the AVCaptureVideoPreviewLayer’s connection with other session manipulation. + + [[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] setVideoOrientation:(AVCaptureVideoOrientation)[[UIApplication sharedApplication] statusBarOrientation]]; + }); + } + + AVCaptureDevice *audioDevice = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] firstObject]; + AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error]; + + if (error) { + NSLog(@"%@", error); + } + + if ([session canAddInput:audioDeviceInput]) { + [session addInput:audioDeviceInput]; + } + + AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; + if ([session canAddOutput:movieFileOutput]) { + [session addOutput:movieFileOutput]; + AVCaptureConnection *connection = [movieFileOutput connectionWithMediaType:AVMediaTypeVideo]; + if ([connection isVideoStabilizationSupported]) + //connection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto; + //[connection setEnablesVideoStabilizationWhenAvailable:YES]; + [self setMovieFileOutput:movieFileOutput]; + } + + AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; + if ([session canAddOutput:stillImageOutput]) { + [stillImageOutput setOutputSettings:@{ AVVideoCodecKey : AVVideoCodecJPEG }]; + [session addOutput:stillImageOutput]; + [self setStillImageOutput:stillImageOutput]; + } + }); [self initialize]; - - - - - } - - - -- (void)viewWillAppear:(BOOL)animated -{ - dispatch_async([self sessionQueue], ^{ - [self addObserver:self forKeyPath:@"sessionRunningAndDeviceAuthorized" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:SessionRunningAndDeviceAuthorizedContext]; - [self addObserver:self forKeyPath:@"stillImageOutput.capturingStillImage" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:CapturingStillImageContext]; - [self addObserver:self forKeyPath:@"movieFileOutput.recording" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:RecordingContext]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subjectAreaDidChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:[[self videoDeviceInput] device]]; - - __weak AVCamViewController *weakSelf = self; - [self setRuntimeErrorHandlingObserver:[[NSNotificationCenter defaultCenter] addObserverForName:AVCaptureSessionRuntimeErrorNotification object:[self session] queue:nil usingBlock:^(NSNotification *note) { - AVCamViewController *strongSelf = weakSelf; - dispatch_async([strongSelf sessionQueue], ^{ - // Manually restarting the session since it must have been stopped due to an error. - [[strongSelf session] startRunning]; - [[strongSelf recordButton] setTitle:NSLocalizedString(@"Record", @"Recording button record title") forState:UIControlStateNormal]; - }); - }]]; - [[self session] startRunning]; - }); - +- (void)viewWillAppear:(BOOL)animated { + dispatch_async([self sessionQueue], ^{ + [self addObserver:self forKeyPath:@"sessionRunningAndDeviceAuthorized" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:SessionRunningAndDeviceAuthorizedContext]; + [self addObserver:self forKeyPath:@"stillImageOutput.capturingStillImage" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:CapturingStillImageContext]; + [self addObserver:self forKeyPath:@"movieFileOutput.recording" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:RecordingContext]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subjectAreaDidChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:[[self videoDeviceInput] device]]; + + __weak AVCamViewController *weakSelf = self; + [self setRuntimeErrorHandlingObserver:[[NSNotificationCenter defaultCenter] addObserverForName:AVCaptureSessionRuntimeErrorNotification object:[self session] queue:nil usingBlock: ^(NSNotification *note) { + AVCamViewController *strongSelf = weakSelf; + dispatch_async([strongSelf sessionQueue], ^{ + // Manually restarting the session since it must have been stopped due to an error. + [[strongSelf session] startRunning]; + [[strongSelf recordButton] setTitle:NSLocalizedString(@"Record", @"Recording button record title") forState:UIControlStateNormal]; + }); + }]]; + [[self session] startRunning]; + }); } - -- (void)viewDidDisappear:(BOOL)animated -{ - dispatch_async([self sessionQueue], ^{ - [[self session] stopRunning]; - - [[NSNotificationCenter defaultCenter] removeObserver:self name:AVCaptureDeviceSubjectAreaDidChangeNotification object:[[self videoDeviceInput] device]]; - [[NSNotificationCenter defaultCenter] removeObserver:[self runtimeErrorHandlingObserver]]; - - [self removeObserver:self forKeyPath:@"sessionRunningAndDeviceAuthorized" context:SessionRunningAndDeviceAuthorizedContext]; - [self removeObserver:self forKeyPath:@"stillImageOutput.capturingStillImage" context:CapturingStillImageContext]; - [self removeObserver:self forKeyPath:@"movieFileOutput.recording" context:RecordingContext]; - }); +- (void)viewDidDisappear:(BOOL)animated { + dispatch_async([self sessionQueue], ^{ + [[self session] stopRunning]; + + [[NSNotificationCenter defaultCenter] removeObserver:self name:AVCaptureDeviceSubjectAreaDidChangeNotification object:[[self videoDeviceInput] device]]; + [[NSNotificationCenter defaultCenter] removeObserver:[self runtimeErrorHandlingObserver]]; + + [self removeObserver:self forKeyPath:@"sessionRunningAndDeviceAuthorized" context:SessionRunningAndDeviceAuthorizedContext]; + [self removeObserver:self forKeyPath:@"stillImageOutput.capturingStillImage" context:CapturingStillImageContext]; + [self removeObserver:self forKeyPath:@"movieFileOutput.recording" context:RecordingContext]; + }); } -- (BOOL)prefersStatusBarHidden -{ - return YES; +- (BOOL)prefersStatusBarHidden { + return YES; } - -- (BOOL)shouldAutorotate -{ - // Disable autorotation of the interface when recording is in progress. +- (BOOL)shouldAutorotate { + // Disable autorotation of the interface when recording is in progress. return ![self lockInterfaceRotation]; } -- (NSUInteger)supportedInterfaceOrientations -{ - return UIInterfaceOrientationMaskAll; -// return UIInterfaceOrientationMaskPortrait; +- (NSUInteger)supportedInterfaceOrientations { + return UIInterfaceOrientationMaskAll; + // return UIInterfaceOrientationMaskPortrait; } -- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration -{ - [[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] setVideoOrientation:(AVCaptureVideoOrientation)toInterfaceOrientation]; +- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { + [[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] setVideoOrientation:(AVCaptureVideoOrientation)toInterfaceOrientation]; } -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context -{ - if (context == CapturingStillImageContext) - { - BOOL isCapturingStillImage = [change[NSKeyValueChangeNewKey] boolValue]; - - if (isCapturingStillImage) - { - [self runStillImageCaptureAnimation]; - } - } - else if (context == RecordingContext) - { - BOOL isRecording = [change[NSKeyValueChangeNewKey] boolValue]; - - dispatch_async(dispatch_get_main_queue(), ^{ - if (isRecording) - { - [[self cameraButton] setEnabled:NO]; - [[self recordButton] setTitle:NSLocalizedString(@"Stop", @"Recording button stop title") forState:UIControlStateNormal]; - [[self recordButton] setEnabled:YES]; - } - else - { - [[self cameraButton] setEnabled:YES]; - [[self recordButton] setTitle:NSLocalizedString(@"Record", @"Recording button record title") forState:UIControlStateNormal]; - [[self recordButton] setEnabled:YES]; - } - }); - } - else if (context == SessionRunningAndDeviceAuthorizedContext) - { - BOOL isRunning = [change[NSKeyValueChangeNewKey] boolValue]; - - dispatch_async(dispatch_get_main_queue(), ^{ - if (isRunning) - { - [[self cameraButton] setEnabled:YES]; - [[self recordButton] setEnabled:YES]; - [[self stillButton] setEnabled:YES]; - } - else - { - [[self cameraButton] setEnabled:NO]; - [[self recordButton] setEnabled:NO]; - [[self stillButton] setEnabled:NO]; - } - }); - } - else - { - [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; - } +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { + if (context == CapturingStillImageContext) { + BOOL isCapturingStillImage = [change[NSKeyValueChangeNewKey] boolValue]; + + if (isCapturingStillImage) { + [self runStillImageCaptureAnimation]; + } + } + else if (context == RecordingContext) { + BOOL isRecording = [change[NSKeyValueChangeNewKey] boolValue]; + + dispatch_async(dispatch_get_main_queue(), ^{ + if (isRecording) { + [[self cameraButton] setEnabled:NO]; + [[self recordButton] setTitle:NSLocalizedString(@"Stop", @"Recording button stop title") forState:UIControlStateNormal]; + [[self recordButton] setEnabled:YES]; + } + else { + [[self cameraButton] setEnabled:YES]; + [[self recordButton] setTitle:NSLocalizedString(@"Record", @"Recording button record title") forState:UIControlStateNormal]; + [[self recordButton] setEnabled:YES]; + } + }); + } + else if (context == SessionRunningAndDeviceAuthorizedContext) { + BOOL isRunning = [change[NSKeyValueChangeNewKey] boolValue]; + + dispatch_async(dispatch_get_main_queue(), ^{ + if (isRunning) { + [[self cameraButton] setEnabled:YES]; + [[self recordButton] setEnabled:YES]; + [[self stillButton] setEnabled:YES]; + } + else { + [[self cameraButton] setEnabled:NO]; + [[self recordButton] setEnabled:NO]; + [[self stillButton] setEnabled:NO]; + } + }); + } + else { + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; + } } #pragma mark Actions -- (IBAction)toggleMovieRecording:(id)sender -{ - [[self recordButton] setEnabled:NO]; - - dispatch_async([self sessionQueue], ^{ - if (![[self movieFileOutput] isRecording]) - { - [self setLockInterfaceRotation:YES]; - - if ([[UIDevice currentDevice] isMultitaskingSupported]) - { - // Setup background task. This is needed because the captureOutput:didFinishRecordingToOutputFileAtURL: callback is not received until AVCam returns to the foreground unless you request background execution time. This also ensures that there will be time to write the file to the assets library when AVCam is backgrounded. To conclude this background execution, -endBackgroundTask is called in -recorder:recordingDidFinishToOutputFileURL:error: after the recorded file has been saved. - [self setBackgroundRecordingID:[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]]; - } - - // Update the orientation on the movie file output video connection before starting recording. - [[[self movieFileOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]]; - - // Turning OFF flash for video recording - [AVCamViewController setFlashMode:AVCaptureFlashModeOff forDevice:[[self videoDeviceInput] device]]; - - // Start recording to a temporary file. - NSString *outputFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[@"movie" stringByAppendingPathExtension:@"mov"]]; - [[self movieFileOutput] startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputFilePath] recordingDelegate:self]; - } - else - { - [[self movieFileOutput] stopRecording]; - } - }); +- (IBAction)toggleMovieRecording:(id)sender { + [[self recordButton] setEnabled:NO]; + + dispatch_async([self sessionQueue], ^{ + if (![[self movieFileOutput] isRecording]) { + [self setLockInterfaceRotation:YES]; + + if ([[UIDevice currentDevice] isMultitaskingSupported]) { + // Setup background task. This is needed because the captureOutput:didFinishRecordingToOutputFileAtURL: callback is not received until AVCam returns to the foreground unless you request background execution time. This also ensures that there will be time to write the file to the assets library when AVCam is backgrounded. To conclude this background execution, -endBackgroundTask is called in -recorder:recordingDidFinishToOutputFileURL:error: after the recorded file has been saved. + [self setBackgroundRecordingID:[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]]; + } + + // Update the orientation on the movie file output video connection before starting recording. + [[[self movieFileOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]]; + + // Turning OFF flash for video recording + [AVCamViewController setFlashMode:AVCaptureFlashModeOff forDevice:[[self videoDeviceInput] device]]; + + // Start recording to a temporary file. + NSString *outputFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[@"movie" stringByAppendingPathExtension:@"mov"]]; + [[self movieFileOutput] startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputFilePath] recordingDelegate:self]; + } + else { + [[self movieFileOutput] stopRecording]; + } + }); } -- (IBAction)changeCamera:(id)sender -{ - [[self cameraButton] setEnabled:NO]; - [[self recordButton] setEnabled:NO]; - [[self stillButton] setEnabled:NO]; - - dispatch_async([self sessionQueue], ^{ - AVCaptureDevice *currentVideoDevice = [[self videoDeviceInput] device]; - AVCaptureDevicePosition preferredPosition = AVCaptureDevicePositionUnspecified; - AVCaptureDevicePosition currentPosition = [currentVideoDevice position]; - - switch (currentPosition) - { - case AVCaptureDevicePositionUnspecified: - preferredPosition = AVCaptureDevicePositionBack; - break; - case AVCaptureDevicePositionBack: - preferredPosition = AVCaptureDevicePositionFront; - break; - case AVCaptureDevicePositionFront: - preferredPosition = AVCaptureDevicePositionBack; - break; - } - - AVCaptureDevice *videoDevice = [AVCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:preferredPosition]; - AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil]; +- (IBAction)changeCamera:(id)sender { + [[self cameraButton] setEnabled:NO]; + [[self recordButton] setEnabled:NO]; + [[self stillButton] setEnabled:NO]; + + dispatch_async([self sessionQueue], ^{ + AVCaptureDevice *currentVideoDevice = [[self videoDeviceInput] device]; + AVCaptureDevicePosition preferredPosition = AVCaptureDevicePositionUnspecified; + AVCaptureDevicePosition currentPosition = [currentVideoDevice position]; + + switch (currentPosition) { + case AVCaptureDevicePositionUnspecified: + preferredPosition = AVCaptureDevicePositionBack; + break; + + case AVCaptureDevicePositionBack: + preferredPosition = AVCaptureDevicePositionFront; + break; + + case AVCaptureDevicePositionFront: + preferredPosition = AVCaptureDevicePositionBack; + break; + } + + AVCaptureDevice *videoDevice = [AVCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:preferredPosition]; + AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil]; dispatch_async(dispatch_get_main_queue(), ^{ [self.btnFlash setImage:[UIImage imageNamed:@"icon_flash_auto.png"] forState:UIControlStateNormal]; self.btnFlash.tag = 0; - if ([videoDevice hasTorch] && [videoDevice hasFlash]){ + if ([videoDevice hasTorch] && [videoDevice hasFlash]) { [videoDevice lockForConfiguration:nil]; [videoDevice setTorchMode:NO]; [videoDevice setFlashMode:AVCaptureFlashModeOn]; @@ -394,230 +347,199 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic self.btnFlash.hidden = NO; return; } - if(![videoDevice hasTorch]) - { + if (![videoDevice hasTorch]) { self.btnFlash.hidden = YES; -// self.cameraButton.center = self.btnThumb.center; -// self.btnThumb.center = self.btnFlash.center; + // self.cameraButton.center = self.btnThumb.center; + // self.btnThumb.center = self.btnFlash.center; } - else if([videoDevice hasTorch] && params.bSwitchFlash) - { + else if ([videoDevice hasTorch] && params.bSwitchFlash) { self.btnFlash.hidden = NO; -// self.btnThumb.center = self.cameraButton.center; -// self.cameraButton.center = CGPointMake(self.cameraButton.center.x - fDist, self.cameraButton.center.y); - + // self.btnThumb.center = self.cameraButton.center; + // self.cameraButton.center = CGPointMake(self.cameraButton.center.x - fDist, self.cameraButton.center.y); } }); - - [[self session] beginConfiguration]; - - [[self session] removeInput:[self videoDeviceInput]]; - if ([[self session] canAddInput:videoDeviceInput]) - { - [[NSNotificationCenter defaultCenter] removeObserver:self name:AVCaptureDeviceSubjectAreaDidChangeNotification object:currentVideoDevice]; - - [AVCamViewController setFlashMode:AVCaptureFlashModeAuto forDevice:videoDevice]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subjectAreaDidChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:videoDevice]; - - [[self session] addInput:videoDeviceInput]; - [self setVideoDeviceInput:videoDeviceInput]; - } - else - { - [[self session] addInput:[self videoDeviceInput]]; - } - - [[self session] commitConfiguration]; - - dispatch_async(dispatch_get_main_queue(), ^{ - [[self cameraButton] setEnabled:YES]; - [[self recordButton] setEnabled:YES]; - [[self stillButton] setEnabled:YES]; - - }); + [[self session] beginConfiguration]; + + [[self session] removeInput:[self videoDeviceInput]]; + if ([[self session] canAddInput:videoDeviceInput]) { + [[NSNotificationCenter defaultCenter] removeObserver:self name:AVCaptureDeviceSubjectAreaDidChangeNotification object:currentVideoDevice]; + + [AVCamViewController setFlashMode:AVCaptureFlashModeAuto forDevice:videoDevice]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subjectAreaDidChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:videoDevice]; + + [[self session] addInput:videoDeviceInput]; + [self setVideoDeviceInput:videoDeviceInput]; + } + else { + [[self session] addInput:[self videoDeviceInput]]; + } + + [[self session] commitConfiguration]; + + dispatch_async(dispatch_get_main_queue(), ^{ + [[self cameraButton] setEnabled:YES]; + [[self recordButton] setEnabled:YES]; + [[self stillButton] setEnabled:YES]; + }); }); } -- (IBAction)snapStillImage:(id)sender -{ - dispatch_async([self sessionQueue], ^{ - // Update the orientation on the still image output video connection before capturing. - [[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]]; - - // Flash set to Auto for Still Capture - [AVCamViewController setFlashMode:AVCaptureFlashModeAuto forDevice:[[self videoDeviceInput] device]]; - - // Capture a still image. - [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { - - if (imageDataSampleBuffer) - { - NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; - capturedImage = [[UIImage alloc] initWithData:imageData]; +- (IBAction)snapStillImage:(id)sender { + dispatch_async([self sessionQueue], ^{ + // Update the orientation on the still image output video connection before capturing. + [[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]]; + + // Flash set to Auto for Still Capture + [AVCamViewController setFlashMode:AVCaptureFlashModeAuto forDevice:[[self videoDeviceInput] device]]; + + // Capture a still image. + [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] completionHandler: ^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { + if (imageDataSampleBuffer) { + NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; + capturedImage = [[UIImage alloc] initWithData:imageData]; capturedImageData = imageData; -// [[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:nil]; + // [[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:nil]; [self takePicture]; - - - } - }]; - }); + } + }]; + }); } -- (IBAction)focusAndExposeTap:(UIGestureRecognizer *)gestureRecognizer -{ - CGPoint devicePoint = [(AVCaptureVideoPreviewLayer *)[[self previewView] layer] captureDevicePointOfInterestForPoint:[gestureRecognizer locationInView:[gestureRecognizer view]]]; - [self focusWithMode:AVCaptureFocusModeAutoFocus exposeWithMode:AVCaptureExposureModeAutoExpose atDevicePoint:devicePoint monitorSubjectAreaChange:YES]; +- (IBAction)focusAndExposeTap:(UIGestureRecognizer *)gestureRecognizer { + CGPoint devicePoint = [(AVCaptureVideoPreviewLayer *)[[self previewView] layer] captureDevicePointOfInterestForPoint:[gestureRecognizer locationInView:[gestureRecognizer view]]]; + [self focusWithMode:AVCaptureFocusModeAutoFocus exposeWithMode:AVCaptureExposureModeAutoExpose atDevicePoint:devicePoint monitorSubjectAreaChange:YES]; } -- (void)subjectAreaDidChange:(NSNotification *)notification -{ - CGPoint devicePoint = CGPointMake(.5, .5); - [self focusWithMode:AVCaptureFocusModeContinuousAutoFocus exposeWithMode:AVCaptureExposureModeContinuousAutoExposure atDevicePoint:devicePoint monitorSubjectAreaChange:NO]; +- (void)subjectAreaDidChange:(NSNotification *)notification { + CGPoint devicePoint = CGPointMake(.5, .5); + [self focusWithMode:AVCaptureFocusModeContinuousAutoFocus exposeWithMode:AVCaptureExposureModeContinuousAutoExposure atDevicePoint:devicePoint monitorSubjectAreaChange:NO]; } #pragma mark File Output Delegate -- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error -{ - if (error) - NSLog(@"%@", error); - - [self setLockInterfaceRotation:NO]; - - // Note the backgroundRecordingID for use in the ALAssetsLibrary completion handler to end the background task associated with this recording. This allows a new recording to be started, associated with a new UIBackgroundTaskIdentifier, once the movie file output's -isRecording is back to NO — which happens sometime after this method returns. - UIBackgroundTaskIdentifier backgroundRecordingID = [self backgroundRecordingID]; - [self setBackgroundRecordingID:UIBackgroundTaskInvalid]; - - [[[ALAssetsLibrary alloc] init] writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error) { - if (error) - NSLog(@"%@", error); - - [[NSFileManager defaultManager] removeItemAtURL:outputFileURL error:nil]; - - if (backgroundRecordingID != UIBackgroundTaskInvalid) - [[UIApplication sharedApplication] endBackgroundTask:backgroundRecordingID]; - }]; +- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error { + if (error) + NSLog(@"%@", error); + + [self setLockInterfaceRotation:NO]; + + // Note the backgroundRecordingID for use in the ALAssetsLibrary completion handler to end the background task associated with this recording. This allows a new recording to be started, associated with a new UIBackgroundTaskIdentifier, once the movie file output's -isRecording is back to NO — which happens sometime after this method returns. + UIBackgroundTaskIdentifier backgroundRecordingID = [self backgroundRecordingID]; + [self setBackgroundRecordingID:UIBackgroundTaskInvalid]; + + [[[ALAssetsLibrary alloc] init] writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock: ^(NSURL *assetURL, NSError *error) { + if (error) + NSLog(@"%@", error); + + [[NSFileManager defaultManager] removeItemAtURL:outputFileURL error:nil]; + + if (backgroundRecordingID != UIBackgroundTaskInvalid) + [[UIApplication sharedApplication] endBackgroundTask:backgroundRecordingID]; + }]; } #pragma mark Device Configuration -- (void)focusWithMode:(AVCaptureFocusMode)focusMode exposeWithMode:(AVCaptureExposureMode)exposureMode atDevicePoint:(CGPoint)point monitorSubjectAreaChange:(BOOL)monitorSubjectAreaChange -{ - dispatch_async([self sessionQueue], ^{ - AVCaptureDevice *device = [[self videoDeviceInput] device]; - NSError *error = nil; - if ([device lockForConfiguration:&error]) - { - if ([device isFocusPointOfInterestSupported] && [device isFocusModeSupported:focusMode]) - { - [device setFocusMode:focusMode]; - [device setFocusPointOfInterest:point]; - } - if ([device isExposurePointOfInterestSupported] && [device isExposureModeSupported:exposureMode]) - { - [device setExposureMode:exposureMode]; - [device setExposurePointOfInterest:point]; - } - [device setSubjectAreaChangeMonitoringEnabled:monitorSubjectAreaChange]; - [device unlockForConfiguration]; - } - else - { - NSLog(@"%@", error); - } - }); +- (void)focusWithMode:(AVCaptureFocusMode)focusMode exposeWithMode:(AVCaptureExposureMode)exposureMode atDevicePoint:(CGPoint)point monitorSubjectAreaChange:(BOOL)monitorSubjectAreaChange { + dispatch_async([self sessionQueue], ^{ + AVCaptureDevice *device = [[self videoDeviceInput] device]; + NSError *error = nil; + if ([device lockForConfiguration:&error]) { + if ([device isFocusPointOfInterestSupported] && [device isFocusModeSupported:focusMode]) { + [device setFocusMode:focusMode]; + [device setFocusPointOfInterest:point]; + } + if ([device isExposurePointOfInterestSupported] && [device isExposureModeSupported:exposureMode]) { + [device setExposureMode:exposureMode]; + [device setExposurePointOfInterest:point]; + } + [device setSubjectAreaChangeMonitoringEnabled:monitorSubjectAreaChange]; + [device unlockForConfiguration]; + } + else { + NSLog(@"%@", error); + } + }); } -+ (void)setFlashMode:(AVCaptureFlashMode)flashMode forDevice:(AVCaptureDevice *)device -{ - if ([device hasFlash] && [device isFlashModeSupported:flashMode]) - { - NSError *error = nil; - if ([device lockForConfiguration:&error]) - { - [device setFlashMode:flashMode]; - [device unlockForConfiguration]; - } - else - { - NSLog(@"%@", error); - } - } ++ (void)setFlashMode:(AVCaptureFlashMode)flashMode forDevice:(AVCaptureDevice *)device { + if ([device hasFlash] && [device isFlashModeSupported:flashMode]) { + NSError *error = nil; + if ([device lockForConfiguration:&error]) { + [device setFlashMode:flashMode]; + [device unlockForConfiguration]; + } + else { + NSLog(@"%@", error); + } + } } -+ (AVCaptureDevice *)deviceWithMediaType:(NSString *)mediaType preferringPosition:(AVCaptureDevicePosition)position -{ - NSArray *devices = [AVCaptureDevice devicesWithMediaType:mediaType]; - AVCaptureDevice *captureDevice = [devices firstObject]; - - for (AVCaptureDevice *device in devices) - { - if ([device position] == position) - { - captureDevice = device; - break; - } - } - - return captureDevice; ++ (AVCaptureDevice *)deviceWithMediaType:(NSString *)mediaType preferringPosition:(AVCaptureDevicePosition)position { + NSArray *devices = [AVCaptureDevice devicesWithMediaType:mediaType]; + AVCaptureDevice *captureDevice = [devices firstObject]; + + for (AVCaptureDevice *device in devices) { + if ([device position] == position) { + captureDevice = device; + break; + } + } + + return captureDevice; } #pragma mark UI -- (void)runStillImageCaptureAnimation -{ - dispatch_async(dispatch_get_main_queue(), ^{ - [[[self previewView] layer] setOpacity:0.0]; - [UIView animateWithDuration:.25 animations:^{ - [[[self previewView] layer] setOpacity:1.0]; - }]; - }); +- (void)runStillImageCaptureAnimation { + dispatch_async(dispatch_get_main_queue(), ^{ + [[[self previewView] layer] setOpacity:0.0]; + [UIView animateWithDuration:.25 animations: ^{ + [[[self previewView] layer] setOpacity:1.0]; + }]; + }); } -- (void)checkDeviceAuthorizationStatus -{ - NSString *mediaType = AVMediaTypeVideo; - - [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) { - if (granted) - { - //Granted access to mediaType - [self setDeviceAuthorized:YES]; - } - else - { - //Not granted access to mediaType - dispatch_async(dispatch_get_main_queue(), ^{ - [[[UIAlertView alloc] initWithTitle:@"AVCam!" - message:@"AVCam doesn't have permission to use Camera, please change privacy settings" - delegate:self - cancelButtonTitle:@"OK" - otherButtonTitles:nil] show]; - [self setDeviceAuthorized:NO]; - }); - } - }]; +- (void)checkDeviceAuthorizationStatus { + NSString *mediaType = AVMediaTypeVideo; + + [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler: ^(BOOL granted) { + if (granted) { + //Granted access to mediaType + [self setDeviceAuthorized:YES]; + } + else { + //Not granted access to mediaType + dispatch_async(dispatch_get_main_queue(), ^{ + [[[UIAlertView alloc] initWithTitle:@"AVCam!" + message:@"AVCam doesn't have permission to use Camera, please change privacy settings" + delegate:self + cancelButtonTitle:@"OK" + otherButtonTitles:nil] show]; + [self setDeviceAuthorized:NO]; + }); + } + }]; } + - (IBAction)onTapThumb:(id)sender { UIButton *btnThumb = (UIButton *)sender; self.imgSmallThumbNail.hidden = btnThumb.selected; self.imgBigThumbNail.hidden = !btnThumb.selected; btnThumb.selected = !btnThumb.selected; - } + - (IBAction)onTapCameraFlash:(id)sender { UIButton *btnCameraFlash = (UIButton *)sender; AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; - if(btnCameraFlash.tag == 0) - { + if (btnCameraFlash.tag == 0) { [btnCameraFlash setImage:[UIImage imageNamed:@"icon_flash.png"] forState:UIControlStateNormal]; btnCameraFlash.tag = 1; - if ([device hasTorch] && [device hasFlash]){ + if ([device hasTorch] && [device hasFlash]) { [device lockForConfiguration:nil]; [device setTorchMode:!device.torchActive]; [device setFlashMode:AVCaptureFlashModeOn]; @@ -625,12 +547,11 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic } return; } - else if (btnCameraFlash.tag == 1) - { + else if (btnCameraFlash.tag == 1) { [btnCameraFlash setImage:[UIImage imageNamed:@"icon_flash_auto.png"] forState:UIControlStateNormal]; btnCameraFlash.tag = 0; - if ([device hasTorch] && [device hasFlash]){ + if ([device hasTorch] && [device hasFlash]) { [device lockForConfiguration:nil]; [device setTorchMode:!device.torchActive]; [device setFlashMode:AVCaptureFlashModeOff]; @@ -639,20 +560,19 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic return; } } + - (IBAction)onBack:(id)sender { - _callback(nil); + _callback(nil, @"3", @"Camera closed before takin a picture."); } --(void) addPinchGesture -{ +- (void)addPinchGesture { twoFingerPinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)]; [self.view addGestureRecognizer:twoFingerPinch]; } --(void) addOpacitySlider -{ +- (void)addOpacitySlider { CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI_2 * (-1)); self.opacitySlider.transform = trans; [self.opacitySlider addTarget:self action:@selector(onChangeOpacitySlider) forControlEvents:UIControlEventValueChanged]; @@ -660,9 +580,7 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic self.opacitySlider.value = 1; } --(void) initialize -{ - +- (void)initialize { fDist = self.btnThumb.center.x - self.cameraButton.center.x; capturedImage = [[UIImage alloc] init]; @@ -672,11 +590,15 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic self.capturedImageView.hidden = YES; self.saveBgPanel.hidden = YES; - self.btnDeletePicture.hidden = YES; - self.btnSaveImage.hidden = YES; + self.btnBigDeletePicture.hidden = self.btnDeletePicture.hidden = YES; + self.btnBigSaveImage.hidden = self.btnSaveImage.hidden = YES; - - self.imgBigThumbNail.image = [UIImage imageWithData:self.params.bgImageData]; + CGRect screenBounds = [[UIScreen mainScreen] bounds]; + CGFloat screenScale = [[UIScreen mainScreen] scale]; + CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale); + NSInteger max = (screenSize.width > screenSize.height) ? screenSize.width : screenSize.height; + UIImage *newImage = [self imageWithImage:[UIImage imageWithData:self.params.bgImageData] scaledToMaxWidth:max maxHeight:max]; + self.imgBigThumbNail.image = newImage; self.imgSmallThumbNail.image = [UIImage imageWithData:self.params.bgImageData]; self.btnThumb.hidden = !params.bMiniature; @@ -690,23 +612,22 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic } AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; - if(params.nDefaultFlash == 1) - { + if (params.nDefaultFlash == 1) { [self.btnFlash setImage:[UIImage imageNamed:@"icon_flash.png"] forState:UIControlStateNormal]; self.btnFlash.tag = 1; - if ([device hasTorch] && [device hasFlash]){ + if ([device hasTorch] && [device hasFlash]) { [device lockForConfiguration:nil]; [device setTorchMode:YES]; [device setFlashMode:AVCaptureFlashModeOn]; [device unlockForConfiguration]; } } - else{ + else { [self.btnFlash setImage:[UIImage imageNamed:@"icon_flash_auto.png"] forState:UIControlStateNormal]; self.btnFlash.tag = 0; - if ([device hasTorch] && [device hasFlash]){ + if ([device hasTorch] && [device hasFlash]) { [device lockForConfiguration:nil]; [device setTorchMode:NO]; [device setFlashMode:AVCaptureFlashModeOn]; @@ -714,127 +635,167 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic } } -// if(!params.bSwitchFlash) -// { -// self.cameraButton.center = self.btnThumb.center; -// self.btnThumb.center = self.btnFlash.center; -// } -// if(!params.bMiniature) -// { -// self.cameraButton.center = self.btnThumb.center; -// } - - + // if(!params.bSwitchFlash) + // { + // self.cameraButton.center = self.btnThumb.center; + // self.btnThumb.center = self.btnFlash.center; + // } + // if(!params.bMiniature) + // { + // self.cameraButton.center = self.btnThumb.center; + // } } - -- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation -{ - if(params.bgImageData1) - { +- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { + if (params.bgImageData1) { isRotated = !isRotated; - if(isRotated) - { - self.imgBigThumbNail.image = [UIImage imageWithData:params.bgImageData1]; + CGRect screenBounds = [[UIScreen mainScreen] bounds]; + CGFloat screenScale = [[UIScreen mainScreen] scale]; + CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale); + NSInteger max = (screenSize.width > screenSize.height) ? screenSize.width : screenSize.height; + + if (isRotated) { + UIImage *newImage = [self imageWithImage:[UIImage imageWithData:self.params.bgImageData1] scaledToMaxWidth:max maxHeight:max]; + self.imgBigThumbNail.image = newImage; + // self.imgBigThumbNail.image = [UIImage imageWithData:params.bgImageData1]; self.imgSmallThumbNail.image = [UIImage imageWithData:params.bgImageData1]; } - else{ - self.imgBigThumbNail.image = [UIImage imageWithData:params.bgImageData]; + else { + UIImage *newImage = [self imageWithImage:[UIImage imageWithData:self.params.bgImageData] scaledToMaxWidth:max maxHeight:max]; + self.imgBigThumbNail.image = newImage; + //self.imgBigThumbNail.image = [UIImage imageWithData:params.bgImageData]; self.imgSmallThumbNail.image = [UIImage imageWithData:params.bgImageData]; } } - } - - - -- (void) takePicture -{ +- (void)takePicture { [self setLockInterfaceRotation:YES]; self.capturedImageView.image = capturedImage; self.capturedImageView.hidden = NO; self.saveBgPanel.hidden = NO; - self.btnDeletePicture.hidden = NO; - self.btnSaveImage.hidden = NO; + self.btnBigDeletePicture.hidden = self.btnDeletePicture.hidden = NO; + self.btnBigSaveImage.hidden = self.btnSaveImage.hidden = NO; self.stillButton.hidden = self.btnFlash.hidden = self.cameraButton.hidden = YES; frameBtnThumb = self.btnThumb.frame; self.btnThumb.frame = self.btnFlash.frame; - self.imgSmallThumbNail.frame = CGRectOffset(self.imgSmallThumbNail.frame, 0, -self.saveBgPanel.frame.size.height ); - + self.imgSmallThumbNail.frame = CGRectOffset(self.imgSmallThumbNail.frame, 0, -self.saveBgPanel.frame.size.height); } + - (IBAction)onDeletePicture:(id)sender { [self setLockInterfaceRotation:NO]; capturedImage = nil; self.capturedImageView.image = nil; self.capturedImageView.hidden = YES; + self.btnBigDeletePicture.hidden = self.btnDeletePicture.hidden = YES; + self.btnBigSaveImage.hidden = self.btnSaveImage.hidden = YES; self.saveBgPanel.hidden = YES; - self.btnDeletePicture.hidden = YES; - self.btnSaveImage.hidden = YES; + self.btnThumb.frame = frameBtnThumb; - self.stillButton.hidden = self.btnFlash.hidden = self.cameraButton.hidden = NO; - AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; - if(![device hasTorch]) - { + self.stillButton.hidden = self.btnFlash.hidden = NO; + + //control camera button + self.cameraButton.hidden = !params.bSwitchCamera; + [self.cameraButton setEnabled:params.bSwitchCamera]; + + + //control flash button + if (params.bSwitchFlash) { + AVCaptureDevice *currentVideoDevice = [[self videoDeviceInput] device]; + AVCaptureDevicePosition currentPosition = [currentVideoDevice position]; + AVCaptureDevice *videoDevice = [AVCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:currentPosition]; + + switch (currentPosition) { + case AVCaptureDevicePositionUnspecified: + case AVCaptureDevicePositionBack: + { + if (self.btnFlash.tag == 0) { + [self.btnFlash setImage:[UIImage imageNamed:@"icon_flash_auto.png"] forState:UIControlStateNormal]; + + if ([videoDevice hasTorch] && [videoDevice hasFlash]) { + [videoDevice lockForConfiguration:nil]; + [videoDevice setTorchMode:videoDevice.torchActive]; + [videoDevice setFlashMode:AVCaptureFlashModeOff]; + [videoDevice unlockForConfiguration]; + } + } + else if (self.btnFlash.tag == 1) { + [self.btnFlash setImage:[UIImage imageNamed:@"icon_flash.png"] forState:UIControlStateNormal]; + if ([videoDevice hasTorch] && [videoDevice hasFlash]) { + [videoDevice lockForConfiguration:nil]; + [videoDevice setTorchMode:videoDevice.torchActive]; + [videoDevice setFlashMode:AVCaptureFlashModeOn]; + [videoDevice unlockForConfiguration]; + } + } + break; + } + + case AVCaptureDevicePositionFront: + + break; + } + + if (![videoDevice hasTorch] || ![videoDevice hasFlash]) { + self.btnFlash.hidden = YES; + } + else { + self.btnFlash.hidden = !params.bSwitchFlash; + [self.btnFlash setEnabled:params.bSwitchFlash]; + } + } + else { self.btnFlash.hidden = YES; } - - self.imgSmallThumbNail.frame = CGRectOffset(self.imgSmallThumbNail.frame, 0, self.saveBgPanel.frame.size.height ); - + self.imgSmallThumbNail.frame = CGRectOffset(self.imgSmallThumbNail.frame, 0, self.saveBgPanel.frame.size.height); } + - (IBAction)onSaveImage:(id)sender { - UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; activityIndicator.center = self.view.center; [self.view addSubview:activityIndicator]; [activityIndicator startAnimating]; - if(params.bSaveInGallery) - { + if (params.bSaveInGallery) { [[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:[capturedImage CGImage] orientation:(ALAssetOrientation)[capturedImage imageOrientation] completionBlock:nil]; } [self.view setUserInteractionEnabled:NO]; - _callback([UIImage imageWithData:capturedImageData]); + _callback([UIImage imageWithData:capturedImageData], nil, nil); } -- (void) onChangeOpacitySlider { +- (void)onChangeOpacitySlider { self.imgSmallThumbNail.alpha = self.opacitySlider.value; self.imgBigThumbNail.alpha = self.opacitySlider.value; } -- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer -{ -// NSLog(@"%f", recognizer.scale); +- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer { + // NSLog(@"%f", recognizer.scale); // NSLog(@"Pinch scale: %f", recognizer.scale); - if(self.lockInterfaceRotation) - { + if (self.lockInterfaceRotation) { return; } - AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; - CGFloat fMaxZoomFactor = device.activeFormat.videoMaxZoomFactor; - if(fMaxZoomFactor > 5) - fMaxZoomFactor = 5; + AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; + CGFloat fMaxZoomFactor = device.activeFormat.videoMaxZoomFactor; + if (fMaxZoomFactor > 5) + fMaxZoomFactor = 5; - CGFloat fNewScale = recognizer.scale * device.videoZoomFactor; - if(fNewScale > 1.0f && fNewScale < fMaxZoomFactor) - { - [device lockForConfiguration:nil]; - [device rampToVideoZoomFactor:fNewScale withRate:3]; - [device unlockForConfiguration]; - } + CGFloat fNewScale = recognizer.scale * device.videoZoomFactor; + if (fNewScale > 1.0f && fNewScale < fMaxZoomFactor) { + [device lockForConfiguration:nil]; + [device rampToVideoZoomFactor:fNewScale withRate:3]; + [device unlockForConfiguration]; + } } - - - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } @@ -843,6 +804,29 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic return orientation == UIDeviceOrientationPortrait; } +#pragma mark -Scale image +- (UIImage *)imageWithImage:(UIImage *)image scaledToMaxWidth:(CGFloat)width maxHeight:(CGFloat)height { + CGFloat oldWidth = image.size.width; + CGFloat oldHeight = image.size.height; + + CGFloat scaleFactor = (oldWidth > oldHeight) ? width / oldWidth : height / oldHeight; + + CGFloat newHeight = oldHeight * scaleFactor; + CGFloat newWidth = oldWidth * scaleFactor; + CGSize newSize = CGSizeMake(newWidth, newHeight); + + if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { + UIGraphicsBeginImageContextWithOptions(newSize, NO, [[UIScreen mainScreen] scale]); + } + else { + UIGraphicsBeginImageContext(newSize); + } + [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; + UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + + return newImage; +} @end diff --git a/src/ios/classes/AVCamViewController_iPad.xib b/src/ios/classes/AVCamViewController_iPad.xib old mode 100644 new mode 100755 index 7cdd6df..ea86ee8 --- a/src/ios/classes/AVCamViewController_iPad.xib +++ b/src/ios/classes/AVCamViewController_iPad.xib @@ -1,12 +1,14 @@ - + - - + + + + @@ -37,7 +39,7 @@ - + + + + + - - @@ -94,7 +118,7 @@ - + + + + - - + + + + - -