自定义UIActivityViewController

时间:2013-08-09 13:13:52

标签: iphone ios uiactivityviewcontroller

我正在尝试在我的UIActivityViewController中将屏幕截图附加到我的邮件中,而不会将屏幕截图保存到我的库中。到目前为止,这是我的代码:

-(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"];

电子邮件没有附上屏幕截图。

当我在UIActivityViewcontroller中按取消按钮时,如何创建动作?

1 个答案:

答案 0 :(得分: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");
         } 
     }];