I have a UIImagePickerController (source type camera) that I use to take pictures. I have it put properly in my .h (added the #property) and .m (#synthesize). Here's what I use to show it:
if (thePicker == nil) {
thePicker = [[UIImagePickerController alloc] init];
thePicker.delegate = self;
thePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
thePicker.allowsEditing = NO;
}
[self presentModalViewController:thePicker animated:YES];
I'm having an odd problem. Every now and again, after closing/opening it a few times, the camera button OR the used button won't work (but the retake and cancel buttons work). I'm not getting any memory warnings and I have a dealloc and didReceiveMemoryWarnings void statement, but they don't get called.
ANyone else having this problem?
I have seen some apps cover it up with their overlay, but you can't remove it. AVCaptureSession really does sound more appropriate for your purposes. I see one example here:
http://www.musicalgeometry.com/?p=1273
Try this it may help u i guess Thanks!!
Related
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];
I am using the following function to activate either the device camera or the image picker depending on the result of a UIActionSheet. if fromCamera=YES then it works on both iPhone and iPad. if fromCamera=NO then it works on iPhone and the image picker appears. But it crashes on the iPad with the following error: UIStatusBarStyleBlackTranslucent is not available on this device. I already know that the iPad can't display the UIStatusBarStyleBlackTranslucent statusBar, but how do I avoid this crash?
-(void)addPhotoFromCamera:(BOOL)fromCamera{
if(fromCamera){
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else{
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentModalViewController:picker animated:YES];
}
If you set the picker to UIImagePickerControllerSourceTypePhotoLibrary on the iPad, then you must(!) present it in a popoverview, otherwise you get exceptions. I do it like this, to atleast control the size of the popover (the standard size is too small in my opinion):
-(void)openPhotoPicker
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.navigationBar.opaque = true;
//put the image picker in its own container controller, to control its size
UIViewController *containerController = [[UIViewController alloc] init];
containerController.contentSizeForViewInPopover = rightPane.frame.size;
[containerController.view addSubview:imagePicker.view];
//then, put the container controller in the popover
popover = [[UIPopoverController alloc] initWithContentViewController:containerController];
//Actually, I would like to do the following, but iOS doesn't let me:
//[rightPane addSubview:imagePicker.view];
//So, put the popover over my rightPane. You might want to change the parameters to suit your needs.
[popover presentPopoverFromRect:CGRectMake(0.0, 0.0, 10.0,0.0)
inView:rightPane
permittedArrowDirections:UIPopoverArrowDirectionLeft
animated:YES];
//There seems to be some nasty bug because of the added layer (the container controller), so you need to call this now and each time the view rotates (see below)
[imagePicker.view setFrame:containerController.view.frame];
}
I also have the following, to counter a rotation bug:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if(imagePicker!=nil && rightPane.frame.size.width>0)
[imagePicker.view setFrame:imagePicker.view.superview.frame];
}
It ain't perfect, but it is ok for my testing purposes at the moment. I consider writing my own Imagepicker, because I don't like being forced to use the popoverview... but well, that's a different story.
I suspect the UIImagePicker is inheriting the translucent status bar from your Info.plist file or from the currently displayed view controller.
What happens if you make the app not have a translucent status bar?
I was having a similar issue, take a look at my answer here:
https://stackoverflow.com/questions/7677058/uiimagepickercontroller-crash-in-ipad-ios5/7839969#7839969
Ok here is the code that I wrote to display the UIImagePickerController in the camera source. I just declared the myPhotopicker in the header for the property and retain it. Synthesized it in the main code file. Then calling the picker I wrote the code below:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
if (myPhotopicker==nil) {
myPhotopicker = [[UIImagePickerController alloc] init];
myPhotopicker.delegate = self;
}// create once!
myPhotopicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:myPhotopicker animated:NO];
}
Upon calling it, there are a few things that is weird happening to the app.
Sometimes, when there are many apps running in the background (iPhone4), the app would fail to load the camera and crash the app. Though it will load CameraRoll/PhotoAlbums without problem.
If the camera view is able to load (when there are less apps running in the background), tapping the Cancel button on the camera view results in the app rebooting itself (where the Default.png image is shown quickly, and back to the main page - like when we started the app).
I have been trying to figure out this problem, but not sure what to do... pls help.. Thanks.
here is the complete code of image pickercontrol try too find solu. here.
http://www.icodeblog.com/2009/07/28/getting-images-from-the-iphone-photo-library-or-camera-using-uiimagepickercontroller/
Regards,
Shyam Parmar
Rather than your 'create once' logic try creating and releasing each time.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
myPhotopicker = [[UIImagePickerController alloc] init];
myPhotopicker.delegate = self;
myPhotopicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:myPhotopicker animated:NO];
[myPhotopicker release];
}
You should also implement the delegate to remove the modal view controller from view when it is dismissed (if you haven't already).
You should also check that the current class conforms to the UINavigationConrollerDelegate protocol.
Hey I was wondering how I can access the front facing camera. Maybe there's some guide for it?
But I don't want all buttons etc. I just want to access the from facing camera, I don't ant the button to take a photo or anything like that.
You can access the front facing camera like:
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
Check out the UIImagePickerController Class Reference
Just set the cameraDevice property of UIImagePickerController to UIImagePickerControllerCameraDeviceFront. But you should check if the device is available.
You should initiate an AVCaptureSession and specify which AVCaptureDevice to use ( AVCaptureDevicePositionFront in your case).
Start looking for the AVCaptureSession documentation and you should have a better understanding of what to do.
Well, what "Khushbu Shah" said is right. however actually the isCameraDeviceAvailable is available only ios 4 and above. Just to ensure that you opened the front camera only if it is there, the right code block to be used is as follows.
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.delegate = self;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
if([UIImagePickerController respondsToSelector:#selector(isCameraDeviceAvailable:)])
{ //check if iphone 4 and above
if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront])
{
ipc.cameraDevice=UIImagePickerControllerCameraDeviceFront;
}
}
}
[ipc release];
First thing you need to do is to detect if your device has got front-facing camera. For that you need to iterate through the video devices.
Try this method of UIImagePickerController:
+ (BOOL)isCameraDeviceAvailable:(UIImagePickerControllerCameraDevice)cameraDevice
This is a class method and UIImagePickerControllerCameraDevice can take two values:
- UIImagePickerControllerCameraDeviceRear
- UIImagePickerControllerCameraDeviceFront
Example code:
if( [UIImagePickerController isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceFront]){
// do something
}
Update:
This has been answered. It was my own stupidity, possibly not worth reading any more of this question. lol.
Question:
Right so i have this UIViewController(master) subclass, it has a UIImagepickerController(camera), it also has a UIView(overlayView). Master setups the camera to be configured as a camera only with a custom cameraOverlay, hiding the custom controls e.t.c.
All appears to work fine apart from when i try to programatically take a picture. What happens is the overlayView calls the master and this triggers the take picture, then i hear the shutter sound and the iris closes, the camera appears to dismiss itself (i am defiantly not doing this in my code) and then my viewDidAppear gets called in my master again.
Anybody have any idea whats going on ?
-(void)viewDidLoad
{
NSLog(#"loading the view");
//if the camera is on the device
if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
//make one
camera = [[UIImagePickerController alloc] init];
//setup some settings that we need
camera.sourceType = UIImagePickerControllerSourceTypeCamera;
camera.showsCameraControls = NO;
camera.navigationBarHidden = NO;
camera.toolbarHidden = YES;
camera.cameraViewTransform = CGAffineTransformScale(camera.cameraViewTransform, 1.03, 1.03);
//show it
overlayView = [[OverlayView alloc] initWithFrame:CGRectMake(0,0,320,480) withDelegate:self andController:self];
camera.cameraOverlayView = overlayView;
camerashowing=NO;
}
else
{
alert = [[UIAlertView alloc] initWithTitle:#"No Camera Detected" message:#"The camera is broken or your device has no camera. Please close the application" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
-(void)viewDidAppear:(BOOL)animated
{
if (!cameraShowing)
{
NSLog(#"going to show camera");
[self presentModalViewController:camera animated:NO];
camerashowing = YES;
}
}
-(void)releaseShutter
{
[overlayView toolbarShowWarning];
NSLog(#"going to show camera: %#", self);
[camera takePicture];
}
After some help advice from people in the answers i can say that the camera is not being released.
I have also managed to stop the exec_bad_access by stopping it from calling [presentmodal....] for a second time by checking a bool value in the viewDidAppear Method.
I still have the issue where the modal view disapears, any help, again lol ??
I think you're missing a camera.delegate = self;
For any EXC_BAD_ACCESS errors, you are usually trying to send a message to a released object. The BEST way to track these down is use NSZombieEnabled.
This works by never actually releasing an object, but by wrapping it up as a "zombie" and setting a flag inside it that says it normally would have been released. This way, if you try to access it again, it still know what it was before you made the error, and with this little bit of information, you can usually backtrack to see what the issue was.
It especially helps in background threads when the Debugger sometimes craps out on any useful information.
VERY IMPORTANT TO NOTE however, is that you need to 100% make sure this is only in your debug code and not your distribution code. Because nothing is ever released, your app will leak and leak and leak. To remind me to do this, I put this log in my appdelegate:
if(getenv("NSZombieEnabled") || getenv("NSAutoreleaseFreedObjectCheckEnabled"))
NSLog(#"NSZombieEnabled/NSAutoreleaseFreedObjectCheckEnabled enabled!");
If you need help finding the exact line, Do a Build-and-Debug (CMD-Y) instead of a Build-and-Run (CMD-R). When the app crashes, the debugger will show you exactly which line and in combination with NSZombieEnabled, you should be able to find out exactly why.
Check the value of the camera member variable before you try and display it:
NSLog(#"going to show camera: %#", camera);
I suspect it might be being released somewhere, but as coneybeare NSZombieEnabled will let you track it down.