1.创建一个类
.h和.m分别如下...
.h----------------------------------------------------------------
#import#import@interface SharedItem : NSObject-(instancetype)initWithData:(UIImage*)img andFile:(NSURL*)file;
@property (nonatomic, strong) UIImage *img;
@property (nonatomic, strong) NSURL *path;
@end
.m----------------------------------------------------------------
#import "SharedItem.h"
@implementation SharedItem
-(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
------------然后controller分享图片时------------------------------
NSMutableArray *activityItems = [NSMutableArray array];
NSArray *urlArr = [NSArray arrayWithObjects:
@"http://img.bihu.com.cn/0e349bd7c873501926b3317057f7583b.jpg",
@"http://img.bihu.com.cn/0cf3115c64bbac8558730aca0e742e60.jpg",
@"http://img.bihu.com.cn/0cf3115c64bbac8558730aca0e742e60.jpg",
@"http://img.bihu.com.cn/0e349bd7c873501926b3317057f7583b.jpg",
@"http://img.bihu.com.cn/0cf3115c64bbac8558730aca0e742e60.jpg",
@"http://img.bihu.com.cn/0cf3115c64bbac8558730aca0e742e60.jpg",
@"http://img.bihu.com.cn/0cf3115c64bbac8558730aca0e742e60.jpg",
@"http://img.bihu.com.cn/0e349bd7c873501926b3317057f7583b.jpg",
@"http://img.bihu.com.cn/0cf3115c64bbac8558730aca0e742e60.jpg",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSFileManager *fileManager = [NSFileManager defaultManager];
for(int i = 0;i<9;i++)
{
NSString *filestr = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_%d.jpg", i]];
UIImage * imageFromURL = [PicCache getImageFromURL:[NSString stringWithFormat:@"%@",urlArr[i]]];
//[PicCache saveImage:imageFromURL withFileName:filestr ofType:@"jpg"];
[UIImageJPEGRepresentation(image, 0.7) writeToFile:filestr atomically:YES];
UIImage *isImage = [PicCache loadImage:filestr];
NSLog(@"%@",isImage);//检查本地是否保存成功
SharedItem *item = [[SharedItem alloc] initWithData:imageFromURL andFile:[NSURL fileURLWithPath:filestr]];
[activityItems addObject:item];
}
UIActivityViewController *activityView = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityView.modalInPopover = true;
activityView.restorationIdentifier = @"activity";
[self presentViewController:activityView animated:YES completion:nil];