这里我是将生成好的二维码分享到微信好友,并通过识别发送的二维码跳转到app store
将图1的文件,导入自己的工程中。
图1 二维码文件在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);
}];
//创建过滤器
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
//恢复默认
[filter setDefaults];
//给过滤器添加数据
NSString *dataString = @"http://www.pa181.com/";//http://www.520it.com
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
//通过KVO设置滤镜inputMessage数据
[filter setValue:data forKey:@"inputMessage"];
//获取输出的二维码
self.outputImage = [filter outputImage];
//将CIImage转换成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];
// 内容和大小 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:@"关于丁丁_03"];
code.style = MKQRCodeStyleCenterImage;
return [code generateImage];
}
在生成二维码方法里,需要注意的是跳转到app store的地址是https://itunes.apple.com/cn/app/id1216226866,而1216226866是上传app的唯一ID。
#pragma mark ---------------------------- 分享按钮方法 --------------------------------
-(void)shareBtnAction{
NSArray *activityItems = @[self.imageView.image];
//里面initWithActivityItems 传的是item的数组 如果直接用图片数组的话 会经常出现 微信断开的错误
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];
}
网友评论