user1724168
user1724168

Reputation:

Screenshot/Share MapView to Send email-iOS

I have been working on the weather application. I would like to know how to attach/capture existing map to send via email?

Any idea or any useful link.Thanks a lot in advance.

Upvotes: 2

Views: 602

Answers (2)

user1724168
user1724168

Reputation:

You can take screen shot using UIGraphicsGetImageFromCurrentContext.Then you could use this screenshot to attach to email.

- (void)twitterButtonPressed {


    NSString *post=[[NSString alloc]initWithFormat:@"I've burned so far %d Calories today -  update from iPhone app Run Burn Calories!", self.userActivityTotalCount];
    NSURL *url=[NSURL URLWithString:@"www.xxx.uh.edu"];
    UIImage *iconImage2=[self imageWithImage:iconImage scaledToSize:CGSizeMake(73.0, 73.0)];
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *twitterSheet=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        [twitterSheet setInitialText:post];
        [twitterSheet addURL:url];
        [twitterSheet addImage:iconImage2];
        [self presentViewController:twitterSheet animated:YES completion:nil];
        SLComposeViewControllerCompletionHandler completion=^(SLComposeViewControllerResult result){
            switch (result) {
                case SLComposeViewControllerResultDone:
                    NSLog(@"posted successfully!");
                    break;
                case SLComposeViewControllerResultCancelled:
                    NSLog(@" could not posted!");
                    break;

                default:
                    break;
            }
            [twitterSheet dismissViewControllerAnimated:YES completion:nil];

        };
        twitterSheet.completionHandler=completion;

    }
    else{
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"Sorry" message:@"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account set up" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }

}

-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}

Upvotes: 1

Boeckm
Boeckm

Reputation: 3322

First I would convert the existing map to an image if it isn't already in this format. Once you've done that, sending an email is real simple. Here's a snippet of code I have in my application to send an email.

- (void) emailPhoto {

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    // Set the subject of email
    [picker setSubject:@"Subject of the Email"];

    // Add email addresses
    // Notice three sections: "to" "cc" and "bcc"
    //[picker setToRecipients:[NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]];
    //[picker setCcRecipients:[NSArray arrayWithObject:@"[email protected]"]];
    //[picker setBccRecipients:[NSArray arrayWithObject:@"[email protected]"]];

    // Fill out the email body text
    NSString *emailBody = @"Text to appear in the email."; 

    // This is an HTML formatted email
    [picker setMessageBody:emailBody isHTML:NO];

    // Create NSData object as PNG image data from camera image
    // This is where I take my self.workingImage object and convert to data object
    NSData *data = UIImagePNGRepresentation(self.workingImage);

    // Attach image *data* object to the email
    [picker addAttachmentData:data mimeType:@"image/png" fileName:@"Image"];

    // Show email view
    [self presentModalViewController:picker animated:YES];

    // Now the user can send or cancel the email as they please

}

Upvotes: 0

Related Questions