Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.

Commit 9844d75

Browse files
committed
Add option to use front camera for capture (#110)
1 parent 25d3551 commit 9844d75

9 files changed

+52
-2
lines changed

Example/WPMediaPicker/DemoViewController.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ - (void) showPicker:(id) sender
129129
mediaPicker.delegate = self;
130130
mediaPicker.showMostRecentFirst = [self.options[MediaPickerOptionsShowMostRecentFirst] boolValue];
131131
mediaPicker.allowCaptureOfMedia = [self.options[MediaPickerOptionsShowCameraCapture] boolValue];
132+
mediaPicker.preferFrontCamera = [self.options[MediaPickerOptionsPreferFrontCamera] boolValue];
132133
mediaPicker.allowMultipleSelection = [self.options[MediaPickerOptionsAllowMultipleSelection] boolValue];
133134
mediaPicker.modalPresentationStyle = UIModalPresentationPopover;
134135
UIPopoverPresentationController *ppc = mediaPicker.popoverPresentationController;

Example/WPMediaPicker/OptionsViewController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
extern NSString const *MediaPickerOptionsShowMostRecentFirst;
44
extern NSString const *MediaPickerOptionsShowCameraCapture;
5+
extern NSString const *MediaPickerOptionsPreferFrontCamera;
56
extern NSString const *MediaPickerOptionsAllowMultipleSelection;
67
extern NSString const *MediaPickerOptionsPostProcessingStep;
78

Example/WPMediaPicker/OptionsViewController.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
NSString const *MediaPickerOptionsShowMostRecentFirst = @"MediaPickerOptionsShowMostRecentFirst";
44
NSString const *MediaPickerOptionsUsePhotosLibrary = @"MediaPickerOptionsUsePhotosLibrary";
55
NSString const *MediaPickerOptionsShowCameraCapture = @"MediaPickerOptionsShowCameraCapture";
6+
NSString const *MediaPickerOptionsPreferFrontCamera = @"MediaPickerOptionsPreferFrontCamera";
67
NSString const *MediaPickerOptionsAllowMultipleSelection = @"MediaPickerOptionsAllowMultipleSelection";
78
NSString const *MediaPickerOptionsPostProcessingStep = @"MediaPickerOptionsPostProcessingStep";
89

910
typedef NS_ENUM(NSInteger, OptionsViewControllerCell){
1011
OptionsViewControllerCellShowMostRecentFirst,
1112
OptionsViewControllerCellShowCameraCapture,
13+
OptionsViewControllerCellPreferFrontCamera,
1214
OptionsViewControllerCellAllowMultipleSelection,
1315
OptionsViewControllerCellPostProcessingStep,
1416
OptionsViewControllerCellTotal
@@ -18,6 +20,7 @@ @interface OptionsViewController ()
1820

1921
@property (nonatomic, strong) UITableViewCell *showMostRecentFirstCell;
2022
@property (nonatomic, strong) UITableViewCell *showCameraCaptureCell;
23+
@property (nonatomic, strong) UITableViewCell *preferFrontCameraCell;
2124
@property (nonatomic, strong) UITableViewCell *allowMultipleSelectionCell;
2225
@property (nonatomic, strong) UITableViewCell *postProcessingStepCell;
2326

@@ -43,6 +46,10 @@ - (void)viewDidLoad
4346
self.showCameraCaptureCell.accessoryView = [[UISwitch alloc] init];
4447
((UISwitch *)self.showCameraCaptureCell.accessoryView).on = [self.options[MediaPickerOptionsShowCameraCapture] boolValue];
4548
self.showCameraCaptureCell.textLabel.text = @"Show Capture Cell";
49+
50+
self.preferFrontCameraCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
51+
self.preferFrontCameraCell.accessoryView = [[UISwitch alloc] init];
52+
self.preferFrontCameraCell.textLabel.text = @"Prefer Front Camera";
4653

4754
self.allowMultipleSelectionCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
4855
self.allowMultipleSelectionCell.accessoryView = [[UISwitch alloc] init];
@@ -76,6 +83,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
7683
case OptionsViewControllerCellShowCameraCapture:
7784
return self.showCameraCaptureCell;
7885
break;
86+
case OptionsViewControllerCellPreferFrontCamera:
87+
return self.preferFrontCameraCell;
88+
break;
7989
case OptionsViewControllerCellAllowMultipleSelection:
8090
return self.allowMultipleSelectionCell;
8191
break;
@@ -95,6 +105,7 @@ - (void)done:(id) sender
95105
NSDictionary *newOptions = @{
96106
MediaPickerOptionsShowMostRecentFirst:@(((UISwitch *)self.showMostRecentFirstCell.accessoryView).on),
97107
MediaPickerOptionsShowCameraCapture:@(((UISwitch *)self.showCameraCaptureCell.accessoryView).on),
108+
MediaPickerOptionsPreferFrontCamera:@(((UISwitch *)self.preferFrontCameraCell.accessoryView).on),
98109
MediaPickerOptionsAllowMultipleSelection:@(((UISwitch *)self.allowMultipleSelectionCell.accessoryView).on),
99110
MediaPickerOptionsPostProcessingStep:@(((UISwitch *)self.postProcessingStepCell.accessoryView).on)
100111
};

Pod/Classes/WPMediaCapturePreviewCollectionView.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
@interface WPMediaCapturePreviewCollectionView : UICollectionReusableView
44

5+
@property (nonatomic, assign) BOOL preferFrontCamera;
6+
57
- (void)stopCaptureOnCompletion:(void (^)(void))block;
68
- (void)startCapture;
79

Pod/Classes/WPMediaCapturePreviewCollectionView.m

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ - (void)startCapture
8080
self.session = [[AVCaptureSession alloc] init];
8181
self.session.sessionPreset = AVCaptureSessionPreset1280x720;
8282

83-
AVCaptureDevice *device =
84-
[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
83+
AVCaptureDevice *device = [self captureDevice];
8584

8685
NSError *error = nil;
8786
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
@@ -123,4 +122,16 @@ - (NSString *)accessibilityLabel
123122
return NSLocalizedString(@"Camera", @"Accessibility label for the camera tile in the collection view");
124123
}
125124

125+
- (AVCaptureDevice *)captureDevice
126+
{
127+
if (self.preferFrontCamera) {
128+
for (AVCaptureDevice *device in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
129+
if (device.position == AVCaptureDevicePositionFront) {
130+
return device;
131+
}
132+
}
133+
}
134+
return [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
135+
}
136+
126137
@end

Pod/Classes/WPMediaCollectionViewController.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
*/
99
@property (nonatomic, assign) BOOL allowCaptureOfMedia;
1010

11+
/**
12+
If the media picker allows media capturing, it will use the front camera by default when possible
13+
*/
14+
@property (nonatomic, assign) BOOL preferFrontCamera;
15+
1116
/**
1217
If set the picker will show the most recent items on the top left. If not set it will show on the bottom right. Either way it will always scroll to the most recent item when showing the picker.
1318
*/

Pod/Classes/WPMediaCollectionViewController.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ - (instancetype)init
4141
_layout = layout;
4242
_selectedAssets = [[NSMutableArray alloc] init];
4343
_allowCaptureOfMedia = YES;
44+
_preferFrontCamera = NO;
4445
_showMostRecentFirst = NO;
4546
_filter = WPMediaTypeVideoOrImage;
4647
_refreshGroupFirstTime = YES;
@@ -413,6 +414,7 @@ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
413414
UIGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showCapture)];
414415
[self.captureCell addGestureRecognizer:tapGestureRecognizer];
415416
}
417+
self.captureCell.preferFrontCamera = self.preferFrontCamera;
416418
[self.captureCell startCapture];
417419
return self.captureCell;
418420
}
@@ -571,12 +573,22 @@ - (void)showMediaCaptureViewController
571573
imagePickerController.mediaTypes = [mediaTypes allObjects];
572574
imagePickerController.delegate = self;
573575
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
576+
imagePickerController.cameraDevice = [self cameraDevice];
574577
imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
575578
[self presentViewController:imagePickerController animated:YES completion:^{
576579

577580
}];
578581
}
579582

583+
- (UIImagePickerControllerCameraDevice)cameraDevice
584+
{
585+
if (self.preferFrontCamera && [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
586+
return UIImagePickerControllerCameraDeviceFront;
587+
} else {
588+
return UIImagePickerControllerCameraDeviceRear;
589+
}
590+
}
591+
580592
- (void)captureMedia
581593
{
582594
NSString *mediaType = AVMediaTypeVideo;

Pod/Classes/WPMediaPickerViewController.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
*/
2121
@property (nonatomic, assign) BOOL allowCaptureOfMedia;
2222

23+
/**
24+
If the media picker allows media capturing, it will use the front camera by default when possible
25+
*/
26+
@property (nonatomic, assign) BOOL preferFrontCamera;
27+
2328
/**
2429
If set the picker will allow the selection of multiple items. By default this value is YES.
2530
*/

Pod/Classes/WPMediaPickerViewController.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibB
1313
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
1414
if (self) {
1515
_allowCaptureOfMedia = YES;
16+
_preferFrontCamera = NO;
1617
_showMostRecentFirst = NO;
1718
_allowMultipleSelection = YES;
1819
_filter = WPMediaTypeVideoOrImage;
@@ -39,6 +40,7 @@ - (void)setupNavigationController
3940
{
4041
WPMediaCollectionViewController *vc = [[WPMediaCollectionViewController alloc] init];
4142
vc.allowCaptureOfMedia = self.allowCaptureOfMedia;
43+
vc.preferFrontCamera = self.preferFrontCamera;
4244
vc.showMostRecentFirst = self.showMostRecentFirst;
4345
vc.filter = self.filter;
4446
vc.allowMultipleSelection = self.allowMultipleSelection;

0 commit comments

Comments
 (0)