Code is below and crash at [self presentModalViewController:imagePicker animated:YES]; when I use device under iOS6.1. It work fine in iOS 7.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init] ;
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:imagePicker animated:YES];
}
Get the error in console: ImageIO: PNG invalid PNG file: iDOT doesn't point to valid IDAT chunk
Any suggestion is appreciated.
Ok, I have fixed the bug, I want to share it out with detail process.
When it crashes, it shows ImageIO: PNG invalid PNG file: iDOT doesn't point to valid IDAT chunk
libc++abi.dylib: handler threw exception in the console, I have no idea about "ImageIO balabala..." but I think I may catch the exception when I noticed "libc++abi.dylib: handler threw exception". So I add #try #catch in my code, code is below.
#try {
[self presentModalViewController:imagePicker animated:YES] ;
}
#catch (NSException *exception) {
NSLog(#"exception:%#", exception) ;
}
#finally {
}
Then I run it again, I got exception:preferredInterfaceOrientationForPresentation must return a supported interface orientation!.
The problem seems to be a little obvious, after google it, I find a solution which is to override some orientation related methods to provide a preferred interface orientation in UIImagePickerController.
So I subclass the UIImagePickerController and implement some methods like this below:
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
Then run again, no crash , cheers !!!
Replace this line:
[self presentModalViewController:imagePicker animated:YES];
With
[self presentViewController:picker animated:YES completion:nil];
Related
I am presently using UIImagePicker and my app crashes on iOS 8 with the following workflow:
Start the camera, zoom it which shows the zoom slider below and then take a picture. Select Use Photo and app crashes.
After looking more into the crash "didHideZoomSlider" message is sent to the deallocated instance of image picker view.
Here is my code:
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil];
imagePicker.allowsEditing = YES;
imagePicker.delegate = self;
[myController presentViewController:imagePicker animated:animated completion:nil];
I tried a couple of things. My controller holds a strong reference to the image picker, I tried making it a weak reference and the app still crashes. Also I need a strong reference so I actually cannot make it weak.
Although this looks like an Apple bug and someone has already logged it (http://openradar.appspot.com/18762927) I wanted to try the workaround they are using. However I am not able to get to "CAMZoomSlider" through UIImagePicker's instance.
Does anyone know how to get to CAMZoomSlider?
I was able to clear the CAZoomSlider delegate by doing the following in a subclassed UIImagePickerController class. And sure enough, it seems to have resolved the crash.
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self clearZoomSliderDelegate:self.view.subviews];
}
- (void)clearZoomSliderDelegate:(NSArray*)subviews
{
for (UIView *subview in subviews)
{
if ([subview isKindOfClass:NSClassFromString(#"CAMZoomSlider")])
{
if ([subview respondsToSelector:#selector(setDelegate:)]) {
[subview performSelector:#selector(setDelegate:) withObject:nil];
}
return;
}
else if (subview.subviews != nil) {
[self clearZoomSliderDelegate:subview.subviews];
}
}
}
When i try to load camera from my code, camera preview is black. If I wait for 10-20 seconds it will show real camera preview. I found several questions and some of them suggest that running some other code in background should be the reason for this. However I don't have any code running in background.
How should I fix this?
This is my code where I run camera
UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
photoPicker.delegate = self;
photoPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:photoPicker animated:YES completion:NULL];
About 5 months ago my team discovered a memory leak with UIImageViewController in iOS7. Each instantiation slowed down the app exponentially (i.e. first alloc-init had a 1 second delay, second had a 2 second delay, third had a 5 second delay). Eventually, we were having 30-60 delays (similar to what you're experiencing).
We resolved the issue by subclassing UIImagePickerController and making it a Singleton. That way it was only ever initialized once. Now our delay is minimal and we avoid the leak. If subclassing isn't an option, try a class property in your viewController and just lazy load it like so.
-(UIImagePickerController *)imagePicker{
if(!_imagePicker){
_imagePicker = [[UIImagePickerController alloc]init];
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
return _imagePicker;
}
Then you can just call it later like:
[self presentViewController:self.imagePicker animated:YES completion:nil];
Had this myself - it happens if something is running on the main dispatch thread - are you resizing images by any chance?
It puts the preview onto the main thread and if something is using it, you get a black screen. It's a bug and the workaround is to either take over the main thread or to disable the photo picker until the queue is free
This Should work for you:
- (void)cameraViewPickerController:(UIImagePickerController *)picker
{
[self startCameraControllerFromViewController: picker
usingDelegate: self];
}
- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
// Displays a control that allows the user to choose movie capture
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, (NSString *) kUTTypeMovie,nil];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
[controller presentViewController:cameraUI animated:YES completion:nil];
return YES;
}
when i ask to show imagePicker with Photos from PhotoAlbum i get crash:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/9D75B5EC-6CC9-4306-B4A1-88C369FEEC6C/AlterGeo.app> (loaded)' with name 'PLUIPrivacyViewController''
- (void)showImagePicker {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
if ([imagePicker respondsToSelector:#selector(setAllowsEditing:)]) {
[imagePicker setAllowsEditing:_editable];
}
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[imagePicker setDelegate:self];
[[self getNavigationController] presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
- (UINavigationController *)getNavigationController {
if (_delegate && [_delegate isKindOfClass:[UIViewController class]]) {
return [(UIViewController *)_delegate navigationController];
}
return nil;
}
If it first launch and i ios ask for permissions (UIAlertView allow/deny) it will be crash, but if i have already granted permissons (allow) it will be work fine.
How i can resolve it?
I find solution of my problem.
The code is not mine, it was outsource people with strange relisation of project.. anyway they ask for every ViewController xib file in category for UiViewController
- (NSString *)nibName {
if ( ! [NSStringFromClass([self class]) isEqualToAnyString:[NSArray arrayWithObjects:
#"UIStatusBarViewController",
#"UIZoomViewController",
#"PLUIPrivacyViewController",
nil]]) {
return NSStringFromClass([self class]);
}
return nil;
}
and in ios6.0 apple added PLUIPrivacyViewController whitch dont have own nib.. I just add PLUIPrivacyViewController in exceptions..
Ofcource better way to make white list of controllers6 cuz the project is realy old and will not modify, but apple can add some other controllesrs without nibs...
But its lot of time.
I researched a lot on this one. On touching an button, I have to pick an image from device and display on the screen.
But it causes my app to crash when button is touched/pressed.
It crashes from this line of code:
[self presentModalViewController:myPhotopicker animated:YES];
I am developing an iPad app using Xcode 4.2. I am using iPad 5.0 simulator for testing. And my system runs on Mac OS X, version 10.6.8.
Following function is called when button is pressed:
-(IBAction)getPhoto:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
if (myPhotopicker==nil) { myPhotopicker = [[UIImagePickerController alloc] init];
myPhotopicker.delegate = self; }// create once!
myPhotopicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:myPhotopicker animated:YES];
} else {
NSString *str = #"Photo Album is not available!";
}
}
I tried your code and can reproduce the crash in the simulator.
But it is working fine on my iPhone 4 with iOS 4.2.
That said, I made my gallery in the simulator contain some photos.
(Start in-simulator Safari, open some page, save some pictures by long pressing on them and choosing save from the menu.)
Now, the simulator writes
2012-05-08 15:53:55.605 test[5870:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'
to the console.
Okay, read, done:
-(IBAction)getPhoto:(UIButton *)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
if (myPhotopicker==nil) {
myPhotopicker = [[UIImagePickerController alloc] init];
myPhotopicker.delegate = self;
}// create once!
myPhotopicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// iPad Code:
UIPopoverController *popover =
[[UIPopoverController alloc] initWithContentViewController:myPhotopicker];
[popover presentPopoverFromRect:sender.bounds
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
} else {
// iPhone Code:
[self presentModalViewController:myPhotopicker animated:YES];
}
} else {
NSLog(#"Photo Album is not available!");
}
}
And now it looks like that:
I know this thread existed before, but was closed as only appearing in iOS5 beta 6.
By now I have the Golden Master of iOS 5 on my phone and that error still appears.
This is happening when I create a UIImagePickerController with a sourceType of UIImagePickerControllerSourceTypePhotoLibrary more than 5 times. I am, as far as I can tell, creating and releasing the previous UIImagePickerController correctly each time.
Edit: adding code, as requested.
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.delegate = self;
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:ipc animated:YES];
[ipc release];
Later, I call [self dismissModalViewControllerAnimated:YES]; when -imagePickerController:didFinishPickingImage:editingInfo: gets called.
The problem happens in Apple examples, so the best bet is to ignore.
Later, I call [self dismissModalViewControllerAnimated:YES]; when
-imagePickerController:didFinishPickingImage:editingInfo: gets called.
Have you tried to set the delegate of the image picker to nil, in didFinishPickingImage ?
This is not your fault.It may be vary in different version.Apple should solve this issue.Main thing is that you should check for memory leak is important.Thanks
Try this UIImagePickerControllerSourceTypePhotoLibrary Error
I hope it will help
try setting
picker=nil
in the
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissModalViewControllerAnimated:YES];
picker = nil;
}
it works for me......
Try this one, i am sure this is gonna help you;
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self setModalInPopover:YES];
}