ios UIActivityViewController分享功能
新建一个继承至NSObject的shareItem 类
shareItem.h
#import
#import
@interface ShareItem : NSObject
-(instancetype)initWithData:(UIImage*)imgandFile:(NSURL*)file;
@property (nonatomic, strong) UIImage *img;
@property (nonatomic, strong) NSURL *path;
@end
shareItem.m
#import "ShareItem.h"
@implementation ShareItem
-(instancetype)initWithData:(UIImage*)imgandFile:(NSURL*)file
{
self= [superinit];
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*)activityViewControlleritemForActivityType:(NSString*)activityType
{
return_path;
}
-(NSString*)activityViewController:(UIActivityViewController*)activityViewControllersubjectForActivityType:(NSString*)activityType
{
// 这里对我这分享图好像没啥用.... 是的 没啥用....
return@"";
}
@end
保存点击方法
-(void)saveAction{
//这里是我模拟的网络请求到的图片数组
NSArray *activityItems = @[@"http://39.98.122.82/upload/home/c11.jpg",@"http://pic.bizhi360.com/bbpic/70/370.jpg",@"http://pic.bizhi360.com/bbpic/86/6386.jpg",@"http://pic.bizhi360.com/bbpic/69/6569.jpg",@"http://39.98.122.82/upload/home/c11.jpg",@"http://pic.bizhi360.com/bbpic/70/370.jpg",@"http://pic.bizhi360.com/bbpic/86/6386.jpg",@"http://pic.bizhi360.com/bbpic/69/6569.jpg",@"http://39.98.122.82/upload/home/c11.jpg"];
NSMutableArray *items = [NSMutableArray array];
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
for(inti =0; i < activityItems.count; i++) {
//取出地址
NSString *URL = [activityItems[i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//把图片转成NSData类型
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:URL]];
//写入图片中
UIImage*imagerang = [UIImageimageWithData:data];
//图片缓存的地址,自己进行替换
NSString*imagePath = [docPathstringByAppendingString:[NSStringstringWithFormat:@"/ShareWX%d.jpg",i]];
//把图片写进缓存,一定要先写入本地,不然会分享出错
[UIImageJPEGRepresentation(imagerang, .5) writeToFile:imagePath atomically:YES];
//把缓存图片的地址转成NSUrl格式
NSURL*shareobj = [NSURLfileURLWithPath:imagePath];
//这个部分是自定义ActivitySource
ShareItem*item = [[ShareItemalloc]initWithData: imagerangandFile:shareobj];
//分享的数组
[itemsaddObject:item];
}
#pragma mark- 分享功能
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:items applicationActivities:nil];
//去除特定的分享功能
activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook,UIActivityTypePostToTwitter, UIActivityTypePostToWeibo,UIActivityTypeMessage,UIActivityTypeMail,UIActivityTypePrint,UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList,UIActivityTypePostToFlickr,UIActivityTypePostToVimeo,UIActivityTypePostToTencentWeibo,UIActivityTypeAirDrop,UIActivityTypeOpenInIBooks];
[self presentViewController: activityVC animated:YES completion:nil];
}
这里可以看到最多只能分享9张图片,超过9张,就不会显示微信这一项
还有就是大家可以看到不支持多张图片分享到朋友圈,但是可以收藏,然后去微信收藏里点击转存笔记,可以发送至朋友圈,以此来达到多张图片分享到朋友圈的效果,但是这种就是不能全部显示图片,这是唯一的缺点
还需要注意的一点是,上面写的图片是在点击分享按钮的方法中循环的,这样会造成点击分享按钮,半天没有反应的感觉,将图片for循环移出去就好了