Search code examples
objective-cios7screenshot

How to create image from screenshot and send to message interface


I am very new to Objective-C. I am attempting to create an image from a screen shot and send it to the message interface to be sent as a message. I have looked at numerous Stackoverflow questions and one suggests use of snapshotViewAfterScreenUpdates which is new in iOS 7. It returns a UIView pointer but I do not understand how to make an image from that and then to the message interface to be sent as a message. Any help would be greatly appreciated.


Solution

  • UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();   
    UIGraphicsEndImageContext();    
    NSData *newPNG=UIImagePNGRepresentation(img); // or you can use JPG or PDF
    

    To share the image, then you can use UIActivityViewController:

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:@"I would like to share it.",newPNG, nil] applicationActivities:nil];
    activityVC.excludedActivityTypes = @[ UIActivityTypeMessage ,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll];
    [self presentViewController:activityVC animated:YES completion:nil];