Question

When a user taps on a button in my app, I'd like to take a screenshot of the current view and open up a text message with that screenshot image as an attachment. How can I do this in iOS7?

(I've seen posts on how to take a screenshot but not anything on taking a screenshot and attaching it to a message)

Thanks!

Was it helpful?

Solution

1. For taking a screenshot add the QuartzCore framework, you can use UIGraphicsBeginImageContextWithOptions

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *theImageData=UIImageJPEGRepresentation(theImage, 1.0 ); //you can use PNG too

2. For attaching this image in mail, add MessageUI framework in build phase. And use this NSData for attaching, something like this

//Check if mail can be sent
if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;

       // Add NSData you got as screenshot to attachment
       [mailer addAttachmentData:theImageData mimeType:@"image/jpeg" fileName:[NSString stringWithFormat:@"test.jpg"]];  
        [self presentModalViewController:mailer animated:YES];

    }

EDIT:

3. Sending image through SMS

    // Will Work only for iOS 7

    MFMessageComposeViewController* messageComposer = [[MFMessageComposeViewController alloc] init];
     messageComposer.messageComposeDelegate = self; // As mentioned by the OP in comments, we have to set messageComposeDelegate to self.
     messageComposer.recipients = [NSArray arrayWithObject:@"123456789"];

 if([MFMessageComposeViewController canSendText])
  {

    if([MFMessageComposeViewController respondsToSelector:@selector(canSendAttachments)] && [MFMessageComposeViewController canSendAttachments])
    {
        NSString* uti = (NSString*)kUTTypeMessage;
        [messageComposer addAttachmentData:theImageData typeIdentifier:uti filename:@"filename.jpg"];
    }

    [self presentViewController:messageComposer animated:YES completion:nil];
  }

Handle the delegate callbacks from MFMessageComposeViewController

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{

}

OTHER TIPS

To take the ScreenShot you can use Quartz Display Services methods

You can pass the rect as

CGRect = [self bounds];

Then create image using CGDisplayCreateImageForRect

CGImageRef selectedScreenImage;
selectedScreenImage = CGDisplayCreateImageForRect(kCGDirectMainDisplay, rect);

Convert it to NSData

NSData *data = (NSData *)CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(selectedScreenImage)));

And then Attach it to your message using MFMailComposeViewController

if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *cvc = [[MFMailComposeViewController alloc] init];
        cvc.mailComposeDelegate = self;

       [cvc addAttachmentData:data mimeType:@"image/jpeg" fileName:[NSString stringWithFormat:@"YOUR_IMAGE.png"]];  

        [self presentModalViewController:cvc animated:YES];

    }

To send a MMS

- (UIImage *) imageFromViewIniOS7
{
UIImage* image = nil;

UIGraphicsBeginImageContext(contentScrollview.contentSize);
{
    CGPoint savedContentOffset = contentScrollview.contentOffset;
    CGRect savedFrame = contentScrollview.frame;

    contentScrollview.contentOffset = CGPointZero;
    contentScrollview.frame = CGRectMake(0, 0, contentScrollview.contentSize.width, contentScrollview.contentSize.height);
    if ([[NSString versionofiOS] intValue]>=7)
    {
        [contentScrollview drawViewHierarchyInRect:contentScrollview.bounds afterScreenUpdates:YES];

    }
    else
    {
        [contentScrollview.layer renderInContext: UIGraphicsGetCurrentContext()];

    }
    image = UIGraphicsGetImageFromCurrentImageContext();

    contentScrollview.contentOffset = savedContentOffset;
    contentScrollview.frame = savedFrame;
}
UIGraphicsEndImageContext();


return image;
}
-(void)buttonAction
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; 
pasteboard.persistent = YES;
pasteboard.image = [self imageFromViewIniOS7];

NSString *phoneToCall = @"sms:";
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];    

if([MFMessageComposeViewController canSendText]) {
NSMutableString *emailBody = [[NSMutableString alloc] initWithString:@"Your Email Body"];
picker.messageComposeDelegate = self;
picker.recipients = [NSArray arrayWithObject:@"123456789"];
[picker setBody:emailBody];// your recipient number or self for testing
picker.body = emailBody;
NSLog(@"Picker -- %@",picker.body);
[self presentModalViewController:picker animated:YES];
NSLog(@"SMS fired");
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top