سؤال

I'm fairly new to xcode, I am currently trying to make an app that has several view controllers. I want one of the view controllers to load an image (Screen snap shoot) of the other in a PDF format into an email.

So one view controller has the follow code to take a snap shoot of the view

- (void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];

// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();


// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

[aView.layer renderInContext:pdfContext];

// remove PDF rendering context
UIGraphicsEndPDFContext();

// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);

}

And then the other view controller has the follow code in the hopes that it would load the snap shoot above into the body of an email that can then be sent to who ever.

- (IBAction)showEmail:(id)sender {
// Email Subject
NSString *emailTitle = @"Email Title";
// Email Content
NSString *messageBody = @"";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:@"test@hotmail.com"];

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
[mc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

UIImage *myImage = [UIImage imageNamed:@"documentDirectoryFilename.pdf"];
NSData *imageData = UIImagePNGRepresentation(myImage);

[mc addAttachmentData:imageData  mimeType:@"image/pdf" fileName:@"documentDirectoryFilename.pdf"];

// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:   (MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
    case MFMailComposeResultCancelled:
        NSLog(@"Mail cancelled");
        break;
    case MFMailComposeResultSaved:
        NSLog(@"Mail saved");
        break;
    case MFMailComposeResultSent:
        NSLog(@"Mail sent");
        break;
    case MFMailComposeResultFailed:
        NSLog(@"Mail sent failure: %@", [error localizedDescription]);
        break;
    default:
        break;
}

The problem I'm having is that the file is not found and not loaded into the email successfully. Any help will be greatly appreciated.

هل كانت مفيدة؟

المحلول

Hi I actually solved the problem I was having, I had forgotten to import the other view controller in the view controller header file. Sorry for wasting everyone's time. I thought it would be a very easy solution.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top