UIPickerController crashes the app - ios

I have a UIPickerController that gets your pictures and allows you to pick some of them though at the moment when I click the button to activate it the app crashes.
Here is the code that I am using for it:
in my ViewDidLoad method:
pickerController = [[UIImagePickerController alloc] init];
pickerController.allowsEditing = NO;
pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
The function:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:nil];
patientPicture = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
UIImageView *pictureView = (UIImageView *)[imageCell viewWithTag:777];
pictureView.image = patientPicture;
[_imgViewAdd reloadInputViews];
}
And it being called:
- (IBAction)addPicture:(id)sender {
[self presentViewController:pickerController animated:YES completion:nil];
}
It is wierd because I have recently changed my app to Ipad only though while it was in IPhone it worked fine. When you click the button in NSLog this error message crops up which I supose is something to do with it:
UIStatusBarStyleBlackTranslucent is not available on this device.
I suspect this is quite a common issue that people have
Thanks in advance

Try presenting in a popover...
pickerController = [[UIImagePickerController alloc] init];
UIPopoverController *popOverController = [[UIPopoverController alloc] initWithContentViewController:pickerController];
popOverController.delegate = self;
and to present...
[popOverController presentPopoverFromRect:yourframe inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Related

iOS App - Trying to Get iOS photo from gallery but getting an error

I'm new to iOS coding, so my apologies in advance. I know this topic has been covered many times (I searched) however I can't seem to resolve my problem, which is why I am posting.
I am trying to access the iOS photo gallery but I keep getting two errors:
One is
'Application tried to present a nil modal view controller on target .'
Edit: The above error was fixed by initing the _picker in ChooseExsisting, as was suggested in the comments.
The other
[CameraController ChooseExsiting:]: unrecognized selector sent to instance 0x157e11330'
My code is as follows:
- (IBAction)ChooseExsiting {
UIImagePickerController *pickerController = [[UIImagePickerController alloc]
init];
pickerController.delegate = self;
[self.picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:_picker animated:YES completion:NULL]; }
I imagine my ChooseExsisting code is incorrect. Would anyone have any suggestions? I would greatly appreciate it.
In your ChooseExisting method, you instantiate a controller into a local variable, but then call present with _picker property variable which is probably nil. Either present the controller from the local variable or init the property like in TakePhoto method.
EDIT: As for the second part, both your IBActions have a wrong method signature if you are connecting them to Tap handlers in the Storyboard for example. They should look like this:
- (IBAction) TakePhoto:(id)sender
- (IBAction) ChooseExsiting:(id)sender
Change your ChooseExisitng method to below one:
- (IBAction)ChooseExsiting:(id) sender {
UIImagePickerController *pickerController = [[UIImagePickerController alloc]
init];
pickerController.delegate = self;
[pickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:pickerController animated:YES completion:NULL]; }
or
- (IBAction)ChooseExsiting:(id) sender {
self.picker = [[UIImagePickerController alloc]
init];
self.picker.delegate = self;
[self.picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:self.picker animated:YES completion:NULL]; }
You can try this . It work for me
Follow below three steps1.First of all you need to add below delegate in .h file (UIImagePickerControllerDelegate,UIPickerViewDelegate)
2.Add below three methods in your controller
3. On button click call below methods
here choose photo for open gallary. so on button click call choose photo method
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
CGSize newSize=CGSizeMake(150, 150);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[[info objectForKey:UIImagePickerControllerEditedImage] drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
img_profile_pic.image = newImage;
[self dismissViewControllerAnimated:YES completion:nil];
isProfilePic=TRUE;
}
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.allowsEditing = YES;
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = sourceType;
imagePickerController.delegate = self;
imagePickerController = imagePickerController;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void) takePhoto{
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypeCamera];
}
-(void) choosePhoto{
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
Instead of doing
if ([UIImagePickerController isSourceTypeAvailable:!UIImagePickerControllerSourceTypeCamera]) {
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
[self.picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:_picker animated:YES completion:NULL];
} }
You should do this
if ([UIImagePickerController isSourceTypeAvailable:!UIImagePickerControllerSourceTypeCamera]) {
if(!_picker){
_picker = [[UIImagePickerController alloc] init];
_picker.delegate = self;
[_picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:_picker animated:YES completion:NULL];
}else{
[self presentViewController:_picker animated:YES completion:NULL];
}}}
You should initialize _picker property before presenting it, right now you're trying to initialize local property of VC or you can also present self.picker instead of _picker in your code.

iOS 8 UIImagePickerController allowsEditing crash

Have anyone noticed that if you use UIImagePickerController and allowsEditing = YES in iOS 8, your app will crash after the editing view being presented.
*** -[PUUIImageViewController traitCollection]: message sent to deallocated instance 0x7f8d714ab5e0
I can find few information about this PUUIImageViewController. It seems like a new class in iOS 8.
I've spent 2 days on my code and started thinking this may be a bug in iOS 8.
Any ideas?
Here's my code for opening the UIImagePickerController
- (void)picButtonPressed:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.delegate = self;
self.imagePickerController.allowsEditing = YES;
self.imagePickerController.mediaTypes = #[(NSString *)kUTTypeImage];
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:self.imagePickerController animated:YES completion:^{
}];
}
}
As for UIImagePickerController's delegate:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:^{}];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = info[UIImagePickerControllerEditedImage];
[self dismissViewControllerAnimated:YES completion:^{}];
}
The app crashes after the controller dismiss UIImagePickerController.

ImagePicker loads very long for the second time

I have a problem with iOS ImagePicker. It works correctly, but when I want to take another photo the camera loads for a very long time (first time: 1-2sec, second time and later: 8-10sec). This is how I use it:
- (void)takePhoto {
_imagePicker = [UIImagePickerController new];
_imagePicker.delegate = self;
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePicker.allowsEditing = NO;
[self presentViewController:_imagePicker animated:YES completion:nil];
}
and this is how I get the image:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[_imagePickerView.imageView setImage:image];
}
I don't see there much place for bugs. Of course I implement protocols UIImagePickerControllerDelegate and UINavigationControllerDelegate.
Do you have any ideas how can I figure out what is causing this?
You may want to dismiss the UIImagePickerController after you picked an item by calling
[_imagePicker dismissViewControllerAnimated:YES completion:nil];
It should be fine, now.
Try changing your first block of code to reuse the same imagePicker you have already initialized:
- (void)takePhoto {
if(_imagePicker == nil)
_imagePicker = [UIImagePickerController new];
_imagePicker.delegate = self;
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePicker.allowsEditing = NO;
[self presentViewController:_imagePicker animated:YES completion:nil];
}

iOS: Screen no longer receives Touches after calling dismissModalViewController

I have two views. Very simple set up just to play around with the camera roll.
One view will bring up the camera roll using:
[self presentViewController:self.imagePicker animated:NO completion:nil];
which is triggered by a UIButton. It brings up the camera, everything's fine.
After taking a picture, I am calling the following to dismiss the controller:
[self dismissViewControllerAnimated:NO completion:nil];
Again, everything is working.
But the view that brings up the imagePicker Controller no longer receives touch (the UIButton will no longer brings up the camera again) after the Modal View is dismissed.
It will only start to receive touches again when I switch to another view and come back.
I have been searching for a solution to this problem but have not been successful in finding anything. Thanks in advance.
EDIT (Adding code):
In CameraController.m
This is where brings up the Camera Roll
(Subclass of UIViewController conforming to
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>)
//UIButton that brings up the imagePicker
- (IBAction)useCamera
{
[self prepareImagePicker];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
//Initializing ImagePicker
- (void)prepareImagePicker
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage, nil];
self.imagePicker.delegate = self;
OverlayCameraView *cameraOverlay = [[OverlayCameraView alloc] initWithFrame:(CGRect){{0, 0}, 320, 480} andTheCameraController:self];
self.imagePicker.cameraOverlayView = cameraOverlay;
}
}
//Delegate Method when a picture is taken
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.imagePicker.delegate = nil;
[picker dismissViewControllerAnimated:NO completion:nil];
self.imagePicker = nil;
self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
In OverlayCameraView.m
//Camera Overlay Class (Subclass of UIView)
- (id)initWithFrame:(CGRect)frame andTheCameraController:(CameraController*)cameraController
{
if ((self = [super initWithFrame:frame]))
{
self.camera = cameraController;
//.…2 UIButtons set up
}
return self;
}
//Overlay Cancel Button
- (void) cancel
{
[self.camera dismissViewControllerAnimated:NO completion:nil];
}
//Overlay Take Picture Button
- (void) takeAPicture
{
[self.camera.imagePicker takePicture];
}
You should be calling dismissViewControllerAnimated on the picker not self via the UIImagePickerControllerDelegate methods.
[picker dismissViewControllerAnimated:NO completion:nil];
or if you are keeping a reference to it:
[self.imagePicker dismissViewControllerAnimated:NO completion:nil];
EDIT:
Based on the revised code, I believe
self.imagePicker.cameraOverlayView = cameraOverlay;
should be:
self.imagePicker.cameraOverlayView = cameraOverlay.view;
Also this will cause a +2 reference count and will leak if you are not using ARC:
self.imagePicker = [[UIImagePickerController alloc] init];
It should be for proper reference counting:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
self.imagePicker = picker;
[picker release];

UIPopoverController crash on iPad running iOS 5.0.1?

I am using a UIImagePickerController on iOS 5 with the iPad and my app just got rejected because it crashed on their end. It doesn't crash on my end but I am sure it has to do with the UIPopoverController.
Please just point anything out that looks bad!
To show the UIImagePickerController:
- (IBAction)photoLibraryiPad {
if (popoverController) {
if ([self.popoverController isPopoverVisible]) {
[self.popoverController dismissPopoverAnimated:YES];
[popoverController setDelegate:nil];
[popoverController release];
}
} else {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.allowsEditing = YES;
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:imagePicker];
popoverController.delegate = self;
[self.popoverController presentPopoverFromRect:photolibrarybutton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[imagePicker release];
}
}
}
Then this is where it is crashing on their end:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if ([self.popoverController isPopoverVisible]) {
[self.popoverController dismissPopoverAnimated:YES];
[popoverController setDelegate:nil];
[popoverController release];
}
[noimageAvailable setHidden:YES];
UIImage *originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
[imgPic setImage:originalImage forState:UIControlStateNormal];
// Hide picker selector
[picker dismissModalViewControllerAnimated: YES];
[picker release];
}
Two things:
1) When you release the popover, you should set the delegate to nil. popoverController.delegate = nil before calling [popoverController release]. Chances are if you try to use the picker multiple times, it will crash for you too. The delegate is calling an object that no longer exists.
2) When checking to see if the popover is visible, check to make sure it exists first.

Resources