Very often, we want to preserve the result of our program and allow the user to send the image as an email or post it on social media. Contrary to appearances, this is a much easier operation than it seems. Apple programmers have done a good job and you can easily save a selected view (UIView) as an image, and then attach it to an email.
Below is the ready-made code that you can simply connect to your own button or event. Of course, you need to specify the appropriate view that you want to save as an image.
- (IBAction)email {
if ([MFMailComposeViewController canSendMail]) {
// CREATE MESSAGE
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:@"Tutaj wstawiamy temat naszej wiadomości"];
[mailViewController setMessageBody:@"To jest treść naszej wiadomości. Może być HTML. Przykład ze strony. https://dev.orpi.pl" isHTML:NO];
// CREATE IMAGE FROM VIEW
UIGraphicsBeginImageContext(myGraph.frame.size);
[myGraph.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// ATTACH IMAGE TO MESSAGE
NSData *myData = UIImagePNGRepresentation(screenshot);
[mailViewController addAttachmentData:myData mimeType:@"image/png" fileName:@"jakasnazwa"];
// DISPLAY EMAIL MODAL
[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];
} else {
NSLog(@"Urządzenie nie może obecnie wysłać wiadomości.");
}
}