【问题标题】:How to share image through WhatsApp?如何通过 WhatsApp 分享图片?
【发布时间】:2016-01-08 14:48:57
【问题描述】:

我正在使用此代码,但它不起作用,它如何适应它?
有人帮忙吗?谢谢!

UIImage  *iconImage = [UIImage imageNamed:@"Home01-A01.png"];

NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];

[UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];

_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];

_documentInteractionController.UTI = @"net.whatsapp.image";

_documentInteractionController.delegate = self;

[_documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

【问题讨论】:

  • 你能指定什么不工作吗?你有错误吗?你期望什么,你会得到什么?
  • 它不会出错,但我按下whatsapp图标,应用程序退出。

标签: image whatsapp


【解决方案1】:

您好,您可以使用 UIDocumentationView 控制器在 WhatsApp 上共享您的文件,执行以下操作以实现相同目的,

在您的 viewcontroller.h 文件中

  • 添加代理

UIDocumentInteractionControllerDelegate

  • 像这样声明 UIDocumentInteractionController 对象

UIDocumentInteractionController *controller;

在您的 viewcontroller.m 文件中

  • 为 UIDocumentationView 控制器添加以下委托方法以跟踪成功和失败的状态

    pragma mark - 委托方法

    - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
    
       return  self;
    }    
    - (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
    
       NSLog(@"Starting to send this puppy to %@", application);
    }
    
    - (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {
    
       NSLog(@"We're done sending the document.");
    }
    
    • 添加以下代码以共享您的文件,

      -(void)openDocument {
          // here's a URL from our bundle
          NSURL *documentURL = [[NSBundle mainBundle]URLForResource:@"chat" withExtension:@"png”];// You can set your filename and extention here
      
          // pass it to our document interaction controller
          controller = [[UIDocumentInteractionController alloc]init];
          controller.delegate = self;
          controller.URL = documentURL;
      
          // present the preview
          [controller presentPreviewAnimated:YES];
      }
      

注意:您指定的 URL 必须是本地的。

UIDocumentInteractionController 的目标是共享(本地)文档 在应用程序之间。如果您的文件没有存储在本地,那么您可以 通过将文件存储在临时或本地缓存目录中然后从 主包。

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多