iOS UIActivityViewController原生多图分享

前言:

微信分享,友盟分享,sharesdk分享等等分享时,都只能一张图片带文字,做不到多图分享,这时候百度了一下资料就发现了iOS有一个原生控件UIActivityViewController,可以实现多图分享,在多图分享时不能带文字分享,微信分享扩展限制了。
PS:如果分享的时候发现按钮只剩更多时,那就是你的图片有问题,图片不存在就会出现这种情况。
记录一下代码,与大家共勉。

效果图如下:

iOS UIActivityViewController原生多图分享_第1张图片
分享一.jpeg
iOS UIActivityViewController原生多图分享_第2张图片
分享二.jpeg

一、支持类型

各个activity可以支持的数据类型


iOS UIActivityViewController原生多图分享_第3张图片
支持的数据类型.png

各个activity可以支持的数据类型名称

UIKIT_EXTERN NSString *const UIActivityTypePostToFacebook     NS_AVAILABLE_IOS(6_0);  
UIKIT_EXTERN NSString *const UIActivityTypePostToTwitter      NS_AVAILABLE_IOS(6_0);  
UIKIT_EXTERN NSString *const UIActivityTypePostToWeibo        NS_AVAILABLE_IOS(6_0);    // SinaWeibo  
UIKIT_EXTERN NSString *const UIActivityTypeMessage            NS_AVAILABLE_IOS(6_0);  
UIKIT_EXTERN NSString *const UIActivityTypeMail               NS_AVAILABLE_IOS(6_0);  
UIKIT_EXTERN NSString *const UIActivityTypePrint              NS_AVAILABLE_IOS(6_0);  
UIKIT_EXTERN NSString *const UIActivityTypeCopyToPasteboard   NS_AVAILABLE_IOS(6_0);  
UIKIT_EXTERN NSString *const UIActivityTypeAssignToContact    NS_AVAILABLE_IOS(6_0);  
UIKIT_EXTERN NSString *const UIActivityTypeSaveToCameraRoll   NS_AVAILABLE_IOS(6_0);  
UIKIT_EXTERN NSString *const UIActivityTypeAddToReadingList   NS_AVAILABLE_IOS(7_0);  
UIKIT_EXTERN NSString *const UIActivityTypePostToFlickr       NS_AVAILABLE_IOS(7_0);  
UIKIT_EXTERN NSString *const UIActivityTypePostToVimeo        NS_AVAILABLE_IOS(7_0);  
UIKIT_EXTERN NSString *const UIActivityTypePostToTencentWeibo NS_AVAILABLE_IOS(7_0);  
UIKIT_EXTERN NSString *const UIActivityTypeAirDrop            NS_AVAILABLE_IOS(7_0);  

二、分享注意

UIActivityViewController的功能非常简单,几行代码就可以实现分享。

这个是最基础的文字带图片分享,适用于微博,但是有一些activity是不支持图片文字的分享,比如微信。
NSString *textToShare = @"欢迎关注我!";
UIImage *imageToShare = [UIImage imageNamed:@"wang.png"];
NSArray *activityItems = @[urlToShare,textToShare,imageToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:_downImgsArr applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];

接下来就讲多图分享

做多图分享的时候,分享的图片大小特别要注意,不能太大,一旦图片太大,就不能分享成功,所以有2种解决方式。
(1)分享的类型为UIImage,很有可能会出现分享不成功的形式:当为UIImage时,图片一定不能太大,如果一定要用图片直接分享,先把图片压缩再进行分享。
(2)第二种分享也是我推荐的方法,分享的类型为NSUrl:一定要先把图片下载后缓存到本地,把缓存的Url数组进行分享。但是这种方式需要自定义ActivitySource,不然也有可能出现分享不成功的情况。

三、分享步骤

第一种,分享类型为UIImage
UIImage *imageToShare = [UIImage imageNamed:@"111.jpg"];
UIImage *imageToShare1 = [UIImage imageNamed:@"222.jpg"];
UIImage *imageToShare2= [UIImage imageNamed:@"333.jpg"];
NSArray *itemArr = @[imageToShare,imageToShare1,imageToShare2];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems: itemArr applicationActivities:nil];
 //去除特定的分享功能,excludedActivityTypes 去除哪些类型,这里把部分activity都给去除掉,只剩下相册和微信
activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook,UIActivityTypePostToTwitter, UIActivityTypePostToWeibo,UIActivityTypeMessage,UIActivityTypeMail,UIActivityTypePrint,UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList,UIActivityTypePostToFlickr,UIActivityTypePostToVimeo,UIActivityTypePostToTencentWeibo,UIActivityTypeAirDrop,UIActivityTypeOpenInIBooks];
[self presentViewController: activityVC animated:YES completion:nil];

附:
图片压缩代码http://blog.csdn.net/u014220518/article/details/53465631

第二种,分享类型为NSUrl

把图片写进缓存,把本机图片地址写进数组中

1.activityItems这里是UIImage集合
UIImage *imageToShare = [UIImage imageNamed:@"111.jpg"];
    UIImage *imageToShare1 = [UIImage imageNamed:@"222.jpg"];
    UIImage *imageToShare2= [UIImage imageNamed:@"333.jpg"];
    NSArray *activityItems = @[imageToShare,imageToShare1,imageToShare2];
    
    NSMutableArray *items = [NSMutableArray array];
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];

    for (int i = 0; i < activityItems.count; i++) {
        //图片缓存的地址,自己进行替换
        NSString *imagePath = [docPath stringByAppendingString:[NSString stringWithFormat:@"/ShareWX%d.jpg",i]];
        //把图片写进缓存,一定要先写入本地,不然会分享出错
        [UIImageJPEGRepresentation(activityItems[i], .5) writeToFile:imagePath atomically:YES];
        //把缓存图片的地址转成NSUrl格式
        NSURL *shareobj = [NSURL fileURLWithPath:imagePath];
        //这个部分是自定义ActivitySource
        ShareItem *item = [[ShareItem alloc] initWithData:activityItems[i] andFile:shareobj];
        //分享的数组
        [items addObject:item];
    }
2.activityItems这里是地址链接集合
/**
     图片数组为url的本机缓存地址
     url必须是图片的地址,不是网页的地址
*/
    NSArray *activityItems = @[
                             @"http://img3.duitang.com/uploads/item/201604/24/20160424132044_ZzhuX.jpeg",
                             @"http://v1.qzone.cc/avatar/201408/03/23/44/53de58e5da74c247.jpg%21200x200.jpg",
                             @"http://img4.imgtn.bdimg.com/it/u=1483569741,1992390913&fm=214&gp=0.jpg"];
    NSMutableArray *items = [NSMutableArray array];
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    for (int i = 0; i < activityItems.count; i++) {
        //地址转码,取出地址
        NSString *URL = [activityItems[i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        //把图片转成NSData类型
         NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:URL]];
        //写入图片中
        UIImage *imagerang = [UIImage imageWithData:data];
        //图片缓存的地址,自己进行替换
        NSString *imagePath = [docPath stringByAppendingString:[NSString stringWithFormat:@"/ShareWX%d.jpg",i]];
        //把图片写进缓存,一定要先写入本地,不然会分享出错
        [UIImageJPEGRepresentation(imagerang, .5) writeToFile:imagePath atomically:YES];
        //把缓存图片的地址转成NSUrl格式
        NSURL *shareobj = [NSURL fileURLWithPath:imagePath];
        //这个部分是自定义ActivitySource
        ShareItem *item = [[ShareItem alloc] initWithData: imagerang andFile:shareobj];
        //分享的数组
        [items addObject:item];
    }
3.通过UIActivityViewController进行分享
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems: items applicationActivities:nil];
 //去除特定的分享功能,excludedActivityTypes 去除哪些类型,这里把部分activity都给去除掉,只剩下相册和微信
activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook,UIActivityTypePostToTwitter, UIActivityTypePostToWeibo,UIActivityTypeMessage,UIActivityTypeMail,UIActivityTypePrint,UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList,UIActivityTypePostToFlickr,UIActivityTypePostToVimeo,UIActivityTypePostToTencentWeibo,UIActivityTypeAirDrop,UIActivityTypeOpenInIBooks];
[self presentViewController: activityVC animated:YES completion:nil];
4.这个是自定义UIActivityItemSource对象

ShareItem.h

#import 

@interface ShareItem : NSObject

-(instancetype)initWithData:(UIImage *)img andFile:(NSURL *)file;

@property (nonatomic, strong) UIImage *img;
@property (nonatomic, strong) NSURL *path;

@end

ShareItem.m

#import "ShareItem.h"

@implementation ShareItem

-(instancetype)initWithData:(UIImage *)img andFile:(NSURL *)file
{
    self = [super init];
    if (self) {
        _img = img;
        _path = file;
    }
    return self;
}

-(instancetype)init
{
    //不期望这种初始化方式,所以返回nil了。
    return nil;
}

#pragma mark - UIActivityItemSource
-(id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    return _img;
}

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

-(NSString*)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType
{
    // 这里对我这分享图好像没啥用....   是的 没啥用....
    return @"";
}

@end

代码下载:http://code.cocoachina.com/view/134762

感谢以下文章

利用UIActivityController调用ios 系统自带的分享功能,实现微信发布多图的功能
UIActivityViewController 调用微信分享的问题

你可能感兴趣的:(iOS UIActivityViewController原生多图分享)