ios – UIActivityViewController将图像分享到微信无法正常工作

前端之家收集整理的这篇文章主要介绍了ios – UIActivityViewController将图像分享到微信无法正常工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我使用Apple的UIActivityViewController与WeChat(weixin)共享一些图像时.我发现有时它不起作用.大多数情况下,当我只选择1~3张图像时效果很好,但如果我共享9张图片(微信允许的最大数量),它肯定会失败,控制台会打印出来

2016-04-01 16:14:34.258 EverPhoto[5567:1981394] plugin
com.tencent.xin.sharetimeline interrupted 2016-04-01 16:14:34.258
EverPhoto[5567:1981394] plugin com.tencent.xin.sharetimeline
invalidated

这是代码

__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与WeChat共享9张图像,甚至是原始高清尺寸的系统照片断言.
那么,我该如何解决这个问题呢?

解决方法

由于App Extension的内存限制,微信的共享扩展已终止.
根据Apple的 App Extension Programming Guide:优化效率和性能

Memory limits for running app extensions are significantly lower than the memory limits imposed on a foreground app. On both platforms,the system may aggressively terminate extensions because users want to return to their main goal in the host app. Some extensions may have lower memory limits than others: For example,widgets must be especially efficient because users are likely to have several widgets open at the same time.

1.我创建了9个非常小的图像,并与微信成功分享

- (UIImage *)imageWithColor:(UIColor *)color
{
  CGRect rect = CGRectMake(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

原文链接:https://www.f2er.com/iOS/331342.html

猜你在找的iOS相关文章