Search code examples
iphoneiosxcode4.2mfmailcomposeviewcontroller

iOs Add My Current Screen to email


I am working on a drawing app , where i have to send the current drawing through email. I managed to get the screen shot of the current screen . But how can i send that image through MFMailComposeViewControll email ? here is the code i have used to get the screen shot

    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *currentScreen = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

Solution

  • MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    [controller setSubject:AppName];
    
    NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"]; 
    [controller setBccRecipients:bccRecipients];
    
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *currentScreen = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    
    NSData *imageData = UIImagePNGRepresentation(currentScreen);
    [controller addAttachmentData:imageData mimeType:@"image/png" fileName:@"aaa.png"];
    
    
    [controller setMessageBody:@"body" isHTML:YES];
    
    [self presentModalViewController:controller animated:YES];
    
    [controller release];