這里我是將生成好的二維碼分享到微信好友恬砂,并通過識(shí)別發(fā)送的二維碼跳轉(zhuǎn)到app store
將圖1的文件杠输,導(dǎo)入自己的工程中皆尔。
在VC中双藕,引入頭文件 ?#import "MKQRCode.h"
@interface MyViewController ()
@property(nonatomic,strong)UIImageView *imageView; //聲明屬性--->二維碼圖片
@end
- (void)viewDidLoad {
[super viewDidLoad];
//生成二維碼
self.imageView = [[UIImageView alloc]init];
[self.shareView addSubview:self.imageView];
[self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.shareView.mas_centerX);
make.top.equalTo(self.shareView.mas_top).offset(35);
make.width.mas_offset(170*ScreenWidth/375);
make.height.mas_offset(170*ScreenWidth/375);
}];
//創(chuàng)建過濾器
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
//恢復(fù)默認(rèn)
[filter setDefaults];
//給過濾器添加數(shù)據(jù)
NSString *dataString = @"http://www.pa181.com/";//http://www.520it.com
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
//通過KVO設(shè)置濾鏡inputMessage數(shù)據(jù)
[filter setValue:data forKey:@"inputMessage"];
//獲取輸出的二維碼
self.outputImage = [filter outputImage];
//將CIImage轉(zhuǎn)換成UIImage止状,并放大顯示
self.imageView.image = [self generateImage];
//分享按鈕
UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[self.shareView addSubview:shareBtn];
[shareBtn setBackgroundImage:[UIImage imageNamed:@"2017010309110575"] forState:UIControlStateNormal];
[shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.imageView.mas_bottom).offset(35);
make.centerX.equalTo(self.imageView.mas_centerX);
make.width.mas_offset(172*ScreenWidth/375);
make.height.mas_offset(45*ScreenWidth/375);
}];
[shareBtn addTarget:self action:@selector(shareBtnAction) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark -------- 生成二維碼方法 ----------
- (UIImage *)generateImage
{
MKQRCode *code = [[MKQRCode alloc] init];
// 內(nèi)容和大小? https://github.com/ymkil/MKQRCode
//? ? [code setInfo:@"http://www.pa181.com" withSize:300];
[code setInfo:@"https://itunes.apple.com/cn/app/id1216226866" withSize:300];
code.centerImg = [UIImage imageNamed:@"關(guān)于丁丁_03"];
code.style = MKQRCodeStyleCenterImage;
return [code generateImage];
}
在生成二維碼方法里,需要注意的是跳轉(zhuǎn)到app store的地址是https://itunes.apple.com/cn/app/id1216226866攒霹,而1216226866是上傳app的唯一ID怯疤。
#pragma mark ---------------------------- 分享按鈕方法 --------------------------------
-(void)shareBtnAction{
NSArray *activityItems = @[self.imageView.image];
//里面initWithActivityItems? 傳的是item的數(shù)組? 如果直接用圖片數(shù)組的話 會(huì)經(jīng)常出現(xiàn) 微信斷開的錯(cuò)誤
UIActivityViewController *activityView =[[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
// 選中分享類型
[activityView setCompletionWithItemsHandler:^(NSString * __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
//? ? ? ? NSLog(@"act type %@",activityType);
if (completed) {
NSLog(@"ok");
}else {
NSLog(@"no ok");
}
}];
activityView.restorationIdentifier = @"activity";
[activityView setTitle:@"分享"];
[self presentViewController:activityView animated:TRUE completion:nil];
}