UIImagePickerController appears but camera doesn't start on iOS 5 - ios

I have an app where I present a UIImagePickerController with source type UIImagePickerControllerSourceTypeCamera. Everything works fine unless I leave the app and come back (multitasking is enabled so the app comes back right where it left off) and I present the UIImagePickerController again. It appears on screen but the camera never shows, the animation where the camera is revealed never happens, here is a screenshot:
If I press cancel and present the UIImagePickerController again, the camera will show up fine. So the only time this problem occurs is the first time I present the UIImagePickerController after coming back to the app. Anyone know why this is happening? I'm coding for iOS 5
I'm presenting the UIImagePickerController with:
[self presentViewController:capturePhotoPicker animated:YES completion:nil];
and dismissing it with:
[self dismissViewControllerAnimated:YES completion:nil];
I am using the same UIImagePickerController object each time I present it

I had exactly the same problem and then realized I wasn't releasing the UIImagePickerController after presenting it. The camera now works fine first-time after leaving and returning to the app.
So this is my exact code:
UIImagePickerController *takePhotoController = [[UIImagePickerController alloc] init];
takePhotoController.sourceType = UIImagePickerControllerSourceTypeCamera;
takePhotoController.delegate = self;
[self presentModalViewController:takePhotoController animated:YES];
[takePhotoController release];
It's one of those problems you can spend ages on, and the solution is not that obvious (well, it wasn't to me), so I hope this helps some people!

if you change the -(void)viewDidLoad to - (void)viewDidAppear:(BOOL)animated it fixes the problem. I've spent the last 2 weeks trying to figure this out

Related

UIImagePickerController the take photo button not work, until long press or click many times

I just want to take a picture from camera through my app. But present to the UIImagePickerController, the take photo button does't work, i can see the button is pressed style up and down.But no photo is captured.Until i long press the take button or click many times, then a photo can be captured.
in my project, the current UIViewController obey the UIImagePickerControllerDelegate and UINavigationControllerDelege. But it still does'n make a difference.
the code like this below:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
I try add the code:
picker.modalPresentationStyle = UIModalPresentationCurrentContext;
it does'n work neither.
Please help me. I search for solutions many times all around, but no one can solve my problem.
Thanks!
This question took me much time on it, but i still could not solve after debug many times. So i finally decided to custom the camera. I found a wonderful code in github website, the link is here
anyone interested can have a look.
Finally, i found another similar scene: https://forums.developer.apple.com/message/232522#232522

Using Instruments to trace memory leak Xcode5 and iOS7

I developed my first app and still learning. Just updated Xcode 5 yesterday and my iPhone 5 is running iOS 7.1. I am now working on understanding the Instruments tool to analyze my app. I am using the Leak profile in Instruments and have come across a retain cycle and leak which I can't figure out. Appreciate any help I can get to understand a) how to pinpoint the bug and b) what I did wrong and how to avoid it in the future.
In my app, I am using the camera to take a pic. While the camera is up, I tapped on the screen to focus and that is when I noticed the leak pop up in Instruments. Looks like there is a retain cycle not in my code (if I understand correctly) and a root leak in my code which I don't understand.
I am using ARC, so I should not have to release "picker" in the part of my code that Instruments is pointing me to (part 2 below, with code).
What can I do about the retain cycle. It is not in my code, but am I still the cause of it? Does it stem from the "root leak?" If I am NOT responsible for it, what's should I do about it?
My only code in this stack trace does not help me understand where the leak is coming from. The line is Instruments points me to in my code is [picker dismissViewControllerAnimated:YES completion:nil]; in my imagePickerController:didFinishPickingMediaWithInfo: method. I am pasting the code below.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *pickerImage = [info objectForKey:
UIImagePickerControllerOriginalImage];
_imageView.image = pickerImage;
[picker dismissViewControllerAnimated:YES completion:nil]; // <--- mem-leak
}
EDIT - Code where I create my UIImagePickerController.
- (IBAction)snapPicture:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// If the camera button was tapped AND we have a camera, use it...
if ((sender == _cameraButton) && [UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
} else {
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
imagePicker.delegate = self;
// bring up the image picker view
[self presentViewController:imagePicker animated:YES completion:nil];
}
Here's my theory: what you've encountered is evidently a bug (or flaw) in Apple's own code, and all you can do is laugh maniacally and forget about it.
To test my theory, I ran my own code, written totally separately and at a different time. It does almost the same thing your code does: it lets the user take a picture, and puts the image into the interface. Here's my code:
https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch17p702takeAPicture/ch30p962takeAPicture/ViewController.m
I ran my code under Instruments with the Leaks instrument, and guess what? I saw the same issues you did.
Naturally, I then started searching on Stack Overflow, and all of this turns out to be old ground. For example:
UIImagePickerController memory leak on iOS5
Even Apple's own sample code, called PhotoPicker (or Using UIImagePickerController to Select Pictures and Take Photos) has the same leak.

Laggy presentation of SLServiceTypeFacebook

so I'm making a post to Facebook and want the user to select an image before doing that.
Once the image is picked and I show Facebook. Problem is, when I show Facebook straight away, without the image picking part, it looks fine. When I show it after picking the image, Facebook sharing appears animated, but laggy. As if the action has a very low framerate or something.
Here's my code:
self.portraitPicker = [[UIImagePickerController alloc] init];
#if !(TARGET_IPHONE_SIMULATOR)
self.portraitPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
#endif
[self.portraitPicker setDelegate:self];
self.portraitPicker.allowsEditing = YES;
[self presentViewController:self.portraitPicker animated:YES completion:nil];
Now when this is complete, I show the Facebook posting modally:
[picker dismissViewControllerAnimated:YES completion:^{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
self.facebookController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[self.facebookController setInitialText:#"Some text"];
[facebookController addImage:pickedImage];
[self presentViewController:self.facebookController animated:NO completion:nil];
}
}];
I can do this in the didFinishPickingMediaWithInfo or the imagePickerControllerDidCancel, doesn't matter, the problem still comes up.
Being desperate, I tried:
Showing it on the main thread delayed
Showing it async
Showing it outside the dismiss-block
Showing it from the AppDelegate's rootviewcontroller instead
This is not Facebook-related as it happens with SLServiceTypeTwitter as well as with MFMessageComposeViewController.
Can't seem to do away with the laggy animation that ONLY happens showing the image picker. Does anyone know what's causing this?
Given the lack of response and similar issues, I figured the cause was my own architecture.
It turns out that one of my viewcontrollers' ViewDidAppear was called after the camera picker was displayed (as it became the active viewcontroller and consequently the inactive one, giving control back to the original viewcontroller).
Blocking the background calls and animation that happened in this viewDidAppear resolved the issue. I still don't know why calling it delayed (i.e. 10 seconds, or even 100 seconds into the future, when nothing was going on) still had the laggy appearence show up, but there it is.

UIImagePickerController shows last picture taken instead of camera input

I'm having a strange behaviour within my app.
For taking pictures i'm using the following pretty standard code for displaying the UIImagePickerController:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
It works perfectly fine the first time I tap the button which calls this action. The strange behaviour starts when I tap that button again. The UIImagePickerController starts again BUT it doesnt show the input from the camera anymore. It shows the last picture I've taken.
More Details of this state:
Tapping on the image shows the yellow square of the auto focus. (which it actually uses to focus the camera correctly)
When I tap on the ImageCapture button -> the correct image is taken and presented on the screen.
If I take a picture and press 'Retake' the regular camera image is presented as input.
More weirdness: It has nothing to do with the iPad I'm using. Creating a new example app which only has button which calls the code from above everything works perfectly fine.
I assume it has something to do with the configuration of the app. Therefore I checked everything but could not find any differences which may cause this issue.
Thanks in advance for any advice!
Update:
I do implement the UIImagePickerControllerDelegate in order to dismiss the UIImagePickerController.
In reading the Apple documentation on UIImagePickerController doc here it states that "When the user taps a button to pick a newly-captured or saved image or movie, or cancels the operation, dismiss the image picker using your delegate object. For newly-captured media, your delegate can then save it to the Camera Roll on the device. For previously-saved media, your delegate can then use the image data according to the purpose of your app." Maybe you need to implement the UIImagePickerContriollerDelegate protocol methods and properly dismiss the existing UIImagePickerController object. See UIImagePickerControllerDelegate
I finally did find the issue: It was a category on UIViewController I used somewhere else in the project. As soon as I compiled it with my project, the UIImagePickerController acted weird. So I think I somehow managed to use a method name which is also used internally by UIImagePickerController. It still confuses me a little since the category wasn't used on the UIImagePickerController at all.

iAD - adBannerView rotation

In my iPad app that supports all the device orientation, I add an adBannerView to the main view.
So far so good. It works and the ad rotates as expected.
If I click on a particular ad this is visualized full-screen, and when I close it I get back to my app.
The problem is that if you rotate the device while you are visualizing the full-screen ad, this rotates correctly, but when you close it and come back to the app the view is not rotated.
How to solve this? Please help or I will destroy my iPad! ;-)
Basically, you want the
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
method to be called again...
To do that, when the user closes the iAd, simply execute:
UIViewController *correctOrientation = [[UIViewController alloc]init];
[self presentModalViewController:correctOrientation animated:NO];
[self dismissModalViewControllerAnimated:NO];

Resources