最近的一個需求是如何用app,在微信朋友圈發佈不帶app顯示名稱的多圖。ios
經過一番查找之後找到一個類UIActivityController,能夠調用系統的social.framework中的分享接口。看下面的圖就知道了,這個仍是挺常見的數組
微信發佈多圖微信
借鑑了CSDN上的一個demo,app
裏面自定義了一個實現UIActivityItemSource協議的類 ShareItemide
// // SharedItem.h // xingjiang // // Created by 吳德明 on 16/7/14. // Copyright © 2016年 吳德明. All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface SharedItem : NSObject<UIActivityItemSource> -(instancetype)initWithData:(UIImage*)img andFile:(NSURL*)file; @property (nonatomic, strong) UIImage *img; @property (nonatomic, strong) NSURL *path; @end
實現文件atom
// // SharedItem.m // xingjiang // // Created by 吳德明 on 16/7/14. // Copyright © 2016年 吳德明. All rights reserved. // #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 @""; }
實際調用的代碼code
// // ViewController.m // ShareItemWX // // Created by 吳德明 on 16/7/14. // Copyright © 2016年 吳德明. All rights reserved. // #import "ViewController.h" #import "SharedItem.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom]; Button.frame = CGRectMake(0, 0, 100, 100); Button.center = self.view.center; Button.backgroundColor = [UIColor blueColor]; [Button setTitle:@"分享到微信" forState:UIControlStateNormal]; [Button addTarget:self action:@selector(shareWX) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:Button]; } /** 分享到微信 以九宮格的方式*/ -(void)shareWX{ /** 圖片網址數組*/ NSArray *array_photo = @[@"http://img.meifajia.com/o1aneipt09eCl5bqQp4ifbQdTHlKIJfq.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneipt2fbZm38Zct4DH92p-ez7-fXt.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneiocd24Y6jK8uQA8-8y-47H6vRe7.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneiocdd94h6ld4kQJh8PcpjGSkORS.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneiocdd94h6ld4kQJh8PcpjGSkORS.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneipt09eCl5bqQp4ifbQdTHlKIJfq.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneioccpacV1LVg2AfG9fbYl8zN1So.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneipt0haf1zwepSkxx9okI0W34t05.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneipt09eCl5bqQp4ifbQdTHlKIJfq.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneipt09eCl5bqQp4ifbQdTHlKIJfq.jpg?imageView2/1/w/360/h/480/q/85",@"http://img.meifajia.com/o1aneipt09eCl5bqQp4ifbQdTHlKIJfq.jpg?imageView2/1/w/360/h/480/q/85"]; NSMutableArray *array = [[NSMutableArray alloc]init]; for (int i = 0; i <8 && i<array_photo.count; i++) { NSString *URL = array_photo[i]; NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:URL]]; UIImage *imagerang = [UIImage imageWithData:data]; NSString *path_sandox = NSHomeDirectory(); NSString *imagePath = [path_sandox stringByAppendingString:[NSString stringWithFormat:@"/Documents/ShareWX%d.jpg",i]]; [UIImagePNGRepresentation(imagerang) writeToFile:imagePath atomically:YES]; NSURL *shareobj = [NSURL fileURLWithPath:imagePath]; /** 這裏作個解釋 imagerang : UIimage 對象 shareobj:NSURL 對象 這個方法的實際做用就是 在調起微信的分享的時候 傳遞給他 UIimage對象,在分享的時候 實際傳遞的是 NSURL對象 達到咱們分享九宮格的目的 */ SharedItem *item = [[SharedItem alloc] initWithData:imagerang andFile:shareobj]; [array addObject:item]; } UIActivityViewController *activityViewController =[[UIActivityViewController alloc] initWithActivityItems:array applicationActivities:nil]; //儘可能不顯示其餘分享的選項內容 activityViewController.excludedActivityTypes = @[ UIActivityTypePostToFacebook,UIActivityTypePostToTwitter, UIActivityTypePostToWeibo, UIActivityTypeMessage,UIActivityTypeMail,UIActivityTypePrint,UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList,UIActivityTypePostToFlickr,UIActivityTypePostToVimeo,UIActivityTypePostToTencentWeibo,UIActivityTypeAirDrop,UIActivityTypeOpenInIBooks]; if (activityViewController) { [self presentViewController:activityViewController animated:TRUE completion:nil]; } } @end
最終效果orm
中間用戶的操做步驟對象
app界面接口
第一步
第二步
第三步寫評論
ios 版本9.2.1
微信版本6.3.22