0

I have an Action method to call UIActionsheet to display and from there i want to select a button to take a screenshot of the view and generate email using Mailcomposer.

IBAction method to call uiactionsheet is working perfectly. but pressing a button to take a snapshot doesn't work. any help?

Here is my code

 CGSize imageSize = [[UIScreen mainScreen] bounds].size;
        if (NULL != UIGraphicsBeginImageContextWithOptions)
            UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
        else
            UIGraphicsBeginImageContext(imageSize);

        CGContextRef context = UIGraphicsGetCurrentContext();

        // Iterate over every window from back to front
        for (UIWindow *window in [[UIApplication sharedApplication] windows])
        {
            if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
            {
                // -renderInContext: renders in the coordinate space of the layer,
                // so we must first apply the layer's geometry to the graphics context
                CGContextSaveGState(context);
                // Center the context around the window's anchor point
                CGContextTranslateCTM(context, [window center].x, [window center].y);
                // Apply the window's transform about the anchor point
                CGContextConcatCTM(context, [window transform]);
                // Offset by the portion of the bounds left of and above the anchor point
                CGContextTranslateCTM(context,
                                      -[window bounds].size.width * [[window layer] anchorPoint].x,
                                      -[window bounds].size.height * [[window layer] anchorPoint].y);

                // Render the layer hierarchy to the current context
                [[window layer] renderInContext:context];

                // Restore the context
                CGContextRestoreGState(context);
            }
        }

        // Retrieve the screenshot image
        UIImage *imageForEmail = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

      //  return imageForEmail;



     //       UIImageWriteToSavedPhotosAlbum(imageForEmail, nil, nil, nil);

    MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
    mailController.mailComposeDelegate = self;
    [mailController setSubject:@"Screenshot"];
    NSData *imageDataForEmail = UIImageJPEGRepresentation(imageForEmail, 1.0);
    [mailController addAttachmentData:imageDataForEmail mimeType:@"image/jpeg" fileName:@"ABC.jpg"];

    [self presentViewController:mailController animated:YES completion:NULL];

}
Kalaichelvan
  • 220
  • 3
  • 11
  • Can you add relevant brackets to the `if` and `else` of button index 0. It is not clear if the 2nd `if` is part of the `else` or a new `if` clause. Please see here: http://stackoverflow.com/a/2125078/1359306 – Patrick Mar 01 '13 at 11:31
  • i don't think the problem is due to if statement. even i move the method of taking screen shot directly under the Action method "- (IBAction) shareButtonClicked:(id)sender", it still doesn't work. – Kalaichelvan Mar 01 '13 at 12:15
  • I know it won't fix the problem, is just makes the code easier to read, especially when an `else` statement is involved. – Patrick Mar 01 '13 at 12:47
  • @Patrick, Updated the code by adding necessary brackets. – Kalaichelvan Mar 01 '13 at 12:56
  • oh my bad. Instead of presenting the modal view controller, i am dismissing it. updated the code to present the view controller. thanks Patrick anyway. – Kalaichelvan Mar 01 '13 at 15:00
  • 1
    Finally updated the working code which will take screenshot of the whole screen except status bar. hope it might help someone. – Kalaichelvan Mar 03 '13 at 09:29

1 Answers1

1

You don't set the UIActionSheet's delegate to self. Add UIActionSheetDelegate to your .h and replace the button action code with the code below

-(IBAction) shareButtonClicked:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Take a Screenshot and share with"
                                                     delegate:self
                                            cancelButtonTitle:@"cancel"
                                       destructiveButtonTitle:nil
                                            otherButtonTitles:@"Email", @"Message", @"Facebook", @"Twitter", nil];
    [actionSheet showInView:self.view];
}
Ushan87
  • 1,608
  • 8
  • 15