Can I programmatically turn on the camera flash on a new iPhone 4 device, before taking a picture with -takePicture?
I'm developing a photo taking app for iOS 4 and want to power on the flash light before the user takes a picture, so they can see the effect of the flash in advance.
The problem seems to be that for the flash light to stay on, you'll need to set the torchMode on and this is only possible in 'video mode' (UIImagePickerControllerCameraCaptureModeVideo), while you can only ask the UIImagePickerController to takePicture when it is on 'photo mode' (UIImagePickerControllerCameraCaptureModePhoto).
So, the following works, but only shows the flash light when taking a picture:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = NO;
picker.navigationBarHidden = YES;
picker.toolbarHidden = YES;
picker.mediaTypes = [NSArray arrayWithObjects:#"public.image", nil];
picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
[self presentModalViewController:picker animated:YES];
And this also works (shows the torch the whole time), but then I cannot take a picture.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = NO;
picker.navigationBarHidden = YES;
picker.toolbarHidden = YES;
picker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeMovie, nil];
picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
[self presentModalViewController:picker animated:YES];
When I try the toggleTorch code found here: Turn on torch/flash on iPhone there seems to not be any live video feed in the UIImagePickerController.
Are these UIImagePickerController and AVCaptureSession compatible with each other? or are you supposed to choose for either one or the other?
And does anybody know a workaround to get both the flash mode on (or torchMode) and to be able to takePicture?
Have a look at the WWDC 2010 sessions (specifically 409) where they go into functionality like the one you're looking for.
Essentially you need to move away from UIImagePickerController if you're looking to perform these custom camera functions and move towards AVFoundation classes.
Related
I tried this twice with two different apps and I get the same thing. I have a set up a UIImagePIckerController instance as follows:
- (IBAction)addImage:(UIBarButtonItem *)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
if ([mediaTypes containsObject:(NSString *)kUTTypeImage]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
picker.allowsEditing = NO;
picker.showsCameraControls = NO;
[self presentViewController: picker animated:YES completion:NULL];
}
[..]
}
This was the second. In the first I set up a custom overlay to run the shutter and other functions. Everything runs fine but I keep getting an error on the console:
<Error>: CGAffineTransformInvert: singular matrix.
When I run the app, every time I rotate (or move about which signals a rotate) the device while the camera is up {something happens here}. I tried it on both my iPhone 4 and iPad Mini with the same results. After a lot of digging I found this only happen in the case where
picker.showsCameraControls = NO;
If I put
picker.showsCameraControls = YES;
Then I get no message (though my custom overlay is hidden too). Making sure it wasn't the custom overlay itself I tried leaving that out, and it still gives the error message.
Anybody got any ideas of what I should do about this?
I believe it to be largely benign as Apple's own PhotoPicker sample code generates this warning. Rotation has to do with matrices and while I'm not sure which matrix in particular is getting rotated, it is considered a mathematical violation to perform operations on matrices with a determinant of zero (similar to dividing by zero). Such a matrix is not invertible or 'singular':
http://en.wikipedia.org/wiki/Rotation_matrix
http://en.wikipedia.org/wiki/Determinant
http://en.wikipedia.org/wiki/Singular_matrix#singular
I want my app to only use the front facing camera. So I've implemented the following code.
if ([UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
self.imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
This works the first time I open up the camera (it is using the front facing camera). But if I cancel the photo and then re-enter into the camera it is using the rear facing camera.
With each cancel / re-enter into camera it switches between front and rear cameras...
Is there something I am missing?
Tr this code .I used this code and it is working fine .Use this code either in button action or viewwillappear or viewdidload and dismiss the camera view properly
imgPicker =[[UIImagePickerController alloc] init];
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imgPicker.cameraDevice=UIImagePickerControllerCameraDeviceFront;
imgPicker.showsCameraControls = YES;
imgPicker.allowsEditing = YES;
imgPicker.delegate=self;
[self presentViewController:imgPicker
animated:NO completion:nil];
I was having the same problem and resolved the issue in my case by including autorelease on the call to allocate the UIImagePickerController as shown here
UIImagePickerController *cameraUI = [[[UIImagePickerController alloc] init] autorelease];
is there any way to check video camera capability available on iPhone?
Most of the camera related availability support is exposed through the UIImagePickerController. A bit tricker thing is detection of Video Camera. You can detect the presence of a video camera in a iOS device using the following method.
- (BOOL) isVideoCameraAvailable
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
NSArray *sourceTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
[picker release];
if (![sourceTypes containsObject:(NSString *)kUTTypeMovie ]){
return NO;
}
return YES;
}
Found this code (based on UIDevice) which helped me with this issue:
https://github.com/MugunthKumar/DeviceHelper
I have implemented the code to do video recording feature. It works fine on 3GS device. I want to restrict the video recording based on some timer setting. Lets say, i want to allow user to do video recording only upto 20 seconds or 35 seconds like that. How can i do that? Can i show the timer kind of control on top of media player while recording the video?
Please suggest me.
Here is my code for video recording:
UIImagePickerController *pickerController =
[[[UIImagePickerController alloc] init] autorelease];
pickerController.delegate = self;
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerController.showsCameraControls = YES;
pickerController.mediaTypes = [NSArray arrayWithObject:(id)kUTTypeMovie];
[self presentModalViewController:pickerController animated:YES];
videoMaximumDuration expects an NSTimeInterval, which is a tpyedef for a float value. So you should pass it a float value. Try it like this:
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
pickerController.delegate = self;
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerController.showsCameraControls = YES;
pickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie]; // kUTTypeMovie is actually an NSString.
pickerController.videoMaximumDuration = 30.0f; // limits video length to 30 seconds.
[self presentModalViewController:pickerController animated:YES];
[pickerController release];
UIImagePickerController class has property videoMaximumDuration which works for NSTimeInterval.
By default it's value is set for 10 minutes but you can change it's float value according to your need.
So if you make object of UIImagePickerController class named videoPickerController then you can set timer for capturing video like-
videoPickerController.videoMaximumDuration = 25.0f;
HI,
I need to record video using iPhone. To do that i used UIImagePickerController. I have an iPhone 3G, with me. when i run my code to check if source type available it says device not supported. My OS is 3.2.1, where i am wrong, can some one tell me what i need to change.
Also is there some other way to record video. What i need to do is to get the video stream as it is recorded.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
NSArray *sourceTypes =
[UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
if (![sourceTypes containsObject:(NSString *)kUTTypeVideo ]){
NSLog(#"device not supported");
return;
}
//picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeVideo];
picker.videoQuality = UIImagePickerControllerQualityTypeHigh;
[self presentModalViewController:picker animated:YES];
Thanks,
That is because the 3G isn't supported for video recording.
Lance is correct. The native UIImagePickerController does not work on iPhone 3G. If an app does do video recording on a 3G they are doing a hack like taking multiple photos in succession and manually encoding a video file using the individual images.