7

当我用苹果的UIActivityViewController把几张图片分享到微信(weixin)。我发现有时它不起作用。大部分时候我只选择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

PS我试用了APP Google Photo,发现它的分享功能做得很好。它可以将9张甚至系统照片的原始高清尺寸的图像共享给微信使用UIActivityViewController。那么,我应该如何解决这个问题呢?

4

2 回答 2

5

微信分享扩展因应用扩展内存限制而终止。
根据 Apple 的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.微信分享前可以缩小图片,这里有一些缩放方法

于 2016-05-21T09:20:43.420 回答
0

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

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

这对我有用。

于 2016-07-28T02:21:41.927 回答