Photo Library view stays on screen after choosing an image with UIImagePickerController - objective-c

After I choose a picture through the UIImagePickerController interface from the Photo Library, the Photo Library view stays displayed, even though I've called dismissModelViewControllerAnimated in imagePickerController:didFinishPickingImage:editingInfo.
Has anyone seen this? These are the three relevant methods I'm using:
- (IBAction)choosePictureFromLibrary:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Error accessing Photo Library" message:#"This device does not support a Photo Library." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Picture picked!" message:#"You picked a picture!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker {
[picker dismissModalViewControllerAnimated:YES];
}
I would have thought that calling imagePickerController:didFinishPickingImage:editingInfo would completely dismiss the Photo Library view, but it doesn't seem to. Is there anything else I have to do to make it go away?

You need to access the viewController of the picker not the picker itself. Try this line instead.
[[picker parentViewController] dismissModalViewControllerAnimated:YES];

You can just call
[self dismissModalViewControllerAnimated:YES];
to dismiss any modal view controller on top of the current view.
This makes sense since you present the view controller by calling:
[self presentModalViewController:picker animated:YES];

Just an update to the answers to this
[self dismissModalViewControllerAnimated:YES];
has been deprecated in iOS 6.0 so you now need to use.
[self dismissViewControllerAnimated:YES completion:nil];
Not a huge change but for anyone that looks at this question and they are using iOS 6.0 they will need an updated answer.
[self presentModalViewController:filePicker animated:YES];
has also been deprecated in favor of
[self presentViewController:filePicker animated:YES completion:nil];

Related

Camera not working in landscape mode only project?

I have developed landscape mode only project and UIcamera picture controller using take photo but it crashed when camera launched and it will show this reason: Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil];
imagePicker.allowsEditing = NO;
[self presentModalViewController:imagePicker animated:YES];
// [self presentViewController:imagePicker animated:NO completion:^{
// [imagePicker setShowsCameraControls:YES];
// }];
}else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"No camera available" message: #"Failed to take image" delegate: self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
Try this friends:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No camera available" message:#"Failed to take image" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}

UIModalViewController Hidden behind a UIViewController

I am working with a sliding menu, like you would see in the Facebook or Path app.
In the rear menu I am wanting to allow the user to add an image by using the camera or photo library.
The issue I am having is the main view controller, the one that I slide, is always on top.
The Action Sheet presents just fine, but the modal view controller is hidden partially by the main view controller.
Here's my picker code:
- (void)viewDidLoad
{
[super viewDidLoad];
_UIPicker = [[UIImagePickerController alloc] init];
_UIPicker.delegate = self;
}
- (IBAction)profilePicButtonTapped
{
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:#"Set Up Your Profile Picture" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Camera", #"Photo Library", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet showInView:_rearTableView];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
_UIPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:_UIPicker animated:YES completion:nil];
}
else if (buttonIndex == 1)
{
_UIPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_UIPicker animated:YES completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Error" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage * selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
_profileImageView.image = selectedImage;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
A quick way to solve it would be to hide the main ViewController when you present the picker and show it again once it's dismissed. Assuming you have a reference to the main ViewController, just set it's view.hidden property to YES.
Another way to handle it would be to present it from the main ViewController instead of the rear menu.

iOS Mail Composer Won't Dismiss

I am trying to take a screenshot and email it using the mail composer. Everything works great except the mail composer won't dismiss. This post seems to have the same problem, but the solution provided did not work for me. Can't dismiss the email composer view in iPhone?
- (IBAction)Email:(id)sender {
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * imageData = UIImageJPEGRepresentation(image, 1.0);
if ( [MFMailComposeViewController canSendMail] ) {
MFMailComposeViewController * mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];
mailComposer.delegate = self;
[mailComposer setSubject:#"Risk Assessment"];
[mailComposer addAttachmentData:imageData mimeType:#"image/jpeg" fileName:#"attachment.jpg"];
[self presentModalViewController:mailComposer animated:YES];
}
}
The above code works great. How do I call this bottom portion. It seems like the compiler just skips past it.
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (error){
NSString *errorTitle = #"Mail Error";
NSString *errorDescription = [error localizedDescription];
UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:errorTitle message:errorDescription delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[errorView show];
[errorView release];
}
[controller dismissModalViewControllerAnimated:YES];
}
Thanks in advance.
Try
mailComposer.mailComposeDelegate = self;
instead of
mailComposer.delegate = self;
From the MFMailComposeViewController documentation:
#property(nonatomic,assign) id<MFMailComposeViewControllerDelegate> mailComposeDelegate;
The delegate object is responsible for dismissing the view presented by this view controller at the appropriate time. Therefore, you should always provide a delegate and that object should implement the methods of the MFMailComposeViewControllerDelegate protocol.
I am pretty sure that last line should be
[self dismissModalViewControllerAnimated:YES];
The ViewController that presented the view modally, also dismisses it.

Getting a SIGABRT error in dismissModalViewController

Im receiving a SIGABRT error when I cancel a MFMailComposeViewController, exactly in the dismissViewController, don't work
I have a UIViewController within a UINavigationController.
My Code:
- (void)sendMail {
if ([MFMailComposeViewController canSendMail]) {
controller = [[MFMailComposeViewController alloc] init];
[[controller navigationBar] setTintColor:[UIColor getHexColorWithRGB:#"e2de09" alpha:1.0f]];
controller.mailComposeDelegate = self;
[controller setSubject:#"Subject"];
NSString *msg="xxxxx";
[controller setMessageBody:msg isHTML:YES];
[self presentModalViewController:controller animated:YES];
} else {
NSString * errorString = [NSString stringWithFormat:#"mail account not set."];
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:#"Error:" message:errorString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
}
The presentModalViewController:animated: works correctly.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}
But calling [self dissmissModalViewControllerAnimatedLYES] causes a SIGABRT error.
I think the "dismissModalViewControllerAnimated" should be called on the MFMailComposer object, in your case "controller". Note that it is in fact a view controller.
It's resolved!
I had a viewWillDisappear: method where I was removing views...
This method was being called before of present the ModalView and my ViewController was over released on dissmissModalViewController.
I have changed this method.
Thanks

How can i add a UIImagePickerController controller to my ipad based application to show multiple images?

i am using uiimage picker control in ipad based application.so i am displaying it using the following code:
[self presentModalViewController:myImagePicker animated:YES];
but there is a error that shows
'On iPad, UIImagePickerController must be presented via UIPopoverController'
how can i add a UIImagePickerController controller to my ipad based application to show multiple images.can any one provide me a good way to do it.
Do like this.When you click on a button in button action call this.
UIActionSheet *photoActionSheet = [[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Take Photo", #"Choose Existing",nil];
[photoActionSheet showFromRect:CGRectMake(315, 355, 10, 10) inView:self.view animated:YES];
#pragma mark UIActionSheetDelegate methods
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
imgPicker.delegate = self;
if(buttonIndex == 0){
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imgPicker animated:YES];
}
else if(buttonIndex == 1){
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imgPicker animated:YES];
}
}