0

I have an app that I set to Landscape Left, Landscape Right and Portrait in my Info-plist. However, I am generally operating in a Landscape mode for all of my view controllers and I have set

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;}

Except for the view controller where I have a button that I want to allow the user to send emails.

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{return YES;}

When I try to send an email my app crashes. It works just fine on my iPad. Here is my code for sending the email.

- (IBAction)sendEmail:(id)sender
{
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
        mailComposer.mailComposeDelegate = self;

       [mailComposer setSubject:@"HI!"];

        UIImage *myImage = [UIImage imageNamed:@"myPicture.png"];
        NSData *imageData = UIImagePNGRepresentation(myImage);
        [mailComposer addAttachmentData:imageData mimeType:@"image/png" fileName:@"FullTitle.png"];

       NSString *emailBody = @"Hi there!!";
        [mailComposer setMessageBody:emailBody isHTML:NO]; 

        [self presentModalViewController:mailComposer animated:YES];
    }

    else 
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure" 
                                                        message:@"Your device does not support email function"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                             otherButtonTitles:nil];
        [alert show];
    }
}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: you saved the email message in the drafts folder.");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
            break;
        default:
           NSLog(@"Mail not sent.");
             break;
    }

    [self dismissModalViewControllerAnimated:YES];
}

I'm not sure, but I may have somehow locked myself in Landscape mode and so when I want to send an email, the iPhone wants to rotate to Portrait, but seems is not allowed to. I am not seeing any error messages in my console. Thanks in advance for your help.

ctw
  • 97
  • 1
  • 11
  • what's the crash and is there a crash log? – Michael Dautermann Apr 29 '12 at 15:54
  • My screen turns black and nothing happens. ctw – ctw Apr 29 '12 at 16:06
  • If you change shouldAutorotateToInterfaceOrientation to only allow portrait on that view controller does it work? Also are you running this on the simulator or actual phone? – Joel Apr 29 '12 at 18:22
  • Which iPhone model are you testing on? Have you tried closing **all** apps that are currently in background mode? Have you tried resetting the device? – Till Apr 29 '12 at 19:47
  • It still does not work when I change it to portrait only. i tried it on both an actual phone and simulator. – ctw Apr 29 '12 at 19:53
  • I am using the 4S model. I reset the device and it's still not working. – ctw Apr 29 '12 at 20:33

2 Answers2

1

Your app is probably running out of memory, which is caused by calling UIImagePNGRepresentation(). This function copies the entire image into memory when creating the NSData. The larger the image, the more memory is used. A possible solution to this problem is to use UIImageJPEGRepresentation() instead of UIImagePNGRepresentation() and pass something like 0.6 to the compressionQuality parameter. More info can be found in Apple's Documentation: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIImageJPEGRepresentation

datwelk
  • 962
  • 1
  • 8
  • 20
  • I // out the image to see if it was an issue with myImage data. It still does not work. It tries to rotate to portrait then I get a black screen. – ctw Apr 29 '12 at 20:25
1

One issue that I found in iOs 6.0 that if you don't have any configured mail on device,app will go to crash . Just check it with

if([MFMailComposeViewController canSendMail])
{
       [self presentViewController:yourMailComposer animated:YES completion:nil];

} 

with above check we can avoid crash.Note that I am working on iOs 6.1. Hope will help.

Gagan_iOS
  • 3,248
  • 2
  • 24
  • 41