3

I'm trying to attach a screenshot to my mail in my UIActivityViewController without the screenshot being saved to my library. Here's my code so far:

-(IBAction)ActivityController:(id)sender {{



    NSString *shareString = @"";
    UIImage *shareImage = [UIImage imageNamed:@""];


    NSURL *shareUrl = [NSURL URLWithString:@""];
    NSArray *activityItems = [NSArray arrayWithObjects:shareString,shareImage, shareUrl, nil];
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    activityViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:activityViewController animated:YES completion:nil];

    MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];

    mailController.mailComposeDelegate = self;
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *exportData = UIImageJPEGRepresentation(image ,1.0);
    [mailController addAttachmentData:exportData mimeType:@"image/jpeg" fileName:@"Screenshot.jpeg"];

There is no screenshot attached to the email.

And how do I create an action when I press the cancel button in UIActivityViewcontroller?

4

1 回答 1

2

UIActivityViewController 帮助您轻松共享来自 iOS6 的数据。只需调用它,输入数据并单击要共享的数据。不要初始化 MFMailComposeViewController。

这是适用于 iPad 和 iPhone 的示例通用代码

-(IBAction)actionButton:(id)sender {
  //Popover for iPad
    //if (self.popover) {
    //    if ([self.popover isPopoverVisible]) {
      //      return;
      //  } else {
       //     [self.popover dismissPopoverAnimated:YES];
      //      self.popover = nil;
      //  }
   // }

    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSString *shareText = @"Share Text";
    NSURL *shareURL = [NSURL URLWithString:@"http://shareURL.com"];

    /* iOS 6 sharing, */
    UIActivity *activity = [[UIActivity alloc] init];

    NSArray *activityItems = @[image, shareText, shareURL];
    NSArray *applicationActivities = @[activity];
    NSArray *excludeActivities = @[];

    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
    activityController.excludedActivityTypes = excludeActivities;

    // switch for iPhone and iPad.
   // if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
     //   self.popover = [[UIPopoverController alloc] initWithContentViewController:activityController];
      //  self.popover.delegate = self;
       // [self.popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
   // } else {
        [self presentViewController:activityController animated:YES completion:^{
            NSLog(@"Activity complete");
        }];
   // }


}

编辑 如果您想知道何时单击取消按钮,您需要在 presentViewController 中稍作修改

    [self presentViewController:activityController animated:YES completion:nil];
    [activityController setCompletionHandler:^(NSString *act, BOOL done)
     {
         if (!act) {
             NSLog(@"Cancel");
         } 
     }];
于 2013-08-09T13:32:50.997 回答