【问题标题】:UIActivityViewController to share images to WeChat not workUIActivityViewController 无法将图片分享到微信
【发布时间】:2016-04-01 08:41:56
【问题描述】:

当我用苹果的UIActivityViewController分享几张图片到微信(微信)时。我发现有时它不起作用。大部分时候我只选择1~3张图片效果很好,但是如果我分享9张图片(微信允许的最大数量),肯定会失败,控制台会打印

2016-04-01 16:14:34.258 EverPhoto[5567:1981394] 插件 com.tencent.xin.sharetimeline 中断 2016-04-01 16:14:34.258 EverPhoto[5567:1981394] 插件 com.tencent.xin.sharetimeline 无效

代码如下:

__weak typeof(self) __weakSelf = self;
self.activityViewController = [[UIActivityViewController alloc] initWithActivityItems:self.shareItems applicationActivities:nil];
self.activityViewController.excludedActivityTypes = @[UIActivityTypePostToFacebook,
                                     UIActivityTypePostToTwitter,
                                     UIActivityTypePostToVimeo,
                                     UIActivityTypePostToTencentWeibo,
                                     UIActivityTypePrint,
                                     UIActivityTypeCopyToPasteboard,
                                     UIActivityTypeAssignToContact,
                                     UIActivityTypeSaveToCameraRoll,
                                     UIActivityTypeAddToReadingList,
                                     UIActivityTypePostToFlickr,
                                     ];
self.activityViewController.completionWithItemsHandler = ^(NSString * __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
    DLog(@"shareCompleted : %@", completed ? @"YES" : @"NO")
    __weakSelf.shareItems = nil;
    __weakSelf.activityViewController = nil;
};

[self.containerVc presentViewController:self.activityViewController animated:YES completion:nil];

ShareItems 是实现协议UIActivityItemSource 的自定义对象。

附:我试用了APP Google Photo,发现它的分享功能做得非常好。它可以使用UIActivityViewController将9张甚至原始高清尺寸的系统照片断言到微信。 那么,我该如何解决这个问题呢?

【问题讨论】:

    标签: ios objective-c iphone wechat


    【解决方案1】:

    微信分享扩展因应用扩展内存限制而终止。
    根据苹果App Extension Programming Guide:优化效率和性能

    运行应用扩展的内存限制明显低于前台应用的内存限制。在这两个平台上,系统可能会主动终止扩展,因为用户希望在宿主应用程序中返回他们的主要目标。某些扩展的内存限制可能比其他扩展低:例如,小部件必须特别高效,因为用户可能同时打开多个小部件。

    1.我创建了9张非常小的图片,并成功分享到微信:

    - (UIImage *)imageWithColor:(UIColor *)color
    {
      CGRect rect = CGRectMake(0, 0, 1, 1);
      UIGraphicsBeginImageContext(rect.size);
      CGContextRef context = UIGraphicsGetCurrentContext();
    
      CGContextSetFillColorWithColor(context, [color CGColor]);
      CGContextFillRect(context, rect);
    
      UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
    
      return image;
    }    
    

    2.在微信分享之前可以缩小图片,这里有一些Scale methods

    【讨论】:

    • OP 提到 Google Photo 应用程序能够共享高清图像。这是什么原因?
    【解决方案2】:

    我也有同样的问题。@wj2061 的回答是对的,但不是解决方案。我想你可能用 UIImage 设置了 shareItem。如果您有图像的 fileUrl,请将其设置为 shareItem。如果没有,请先尝试将 UIImage 保存到文件。 在您的 shareItem 类中,返回 fileUrl。

    - (nullable id)activityViewController:(UIActivityViewController*)activityViewController itemForActivityType:(NSString *)activityType{
        return _filePathUrl;
    }
    

    这对我有用。

    【讨论】:

    • 如果有人发布了不正确的解决方案,请举报。
    猜你喜欢
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多