生成二维码的两种方式:一种是利用第三方生成,一种是自己自定义生成。
1.用的第三方是:QRCode, 使用前先下载第三方。
关于第三方和自定义,直接上代码:
#import "ViewController.h"
#import "WGJViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
[btn addTarget:self action:@selector(clickPush) forControlEvents:(UIControlEventTouchUpInside)];
[btn setImage:[UIImage imageNamed:@"test01"] forState:(UIControlStateNormal)];
btn.backgroundColor = [UIColor redColor];
// btn.enabled = NO;
// btn.userInteractionEnabled = NO;
btn.frame = CGRectMake(100, 100, 100, 40);
[self.view addSubview:btn];
}
- (void)clickPush
{
WGJViewController *tee = [[WGJViewController alloc] init];
[self.navigationController pushViewController:tee animated:YES];
}
@end
#import "WGJViewController.h"
#import "UIImage+MDQRCode.h"
#import "UIImageView+WProductCode.h"
@interface WGJViewController ()
@property (nonatomic, strong) UIImageView *imgView;
@end
@implementation WGJViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// Do any additional setup after loading the view.
UIView *vWhiteView = [[UIView alloc] initWithFrame:CGRectMake(100 , 100, 136 , 250)];
vWhiteView.layer.cornerRadius = 2;
vWhiteView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:vWhiteView];
//二维码---1----
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(18 ,18 , CGRectGetWidth(vWhiteView.frame)-36 ,CGRectGetWidth(vWhiteView.frame)-36)];
[vWhiteView addSubview:imageView];
imageView.backgroundColor = [UIColor whiteColor];
imageView.image = [UIImage mdQRCodeForString:[NSString stringWithFormat:@"%@",@"12312"] size:imageView.bounds.size.width fillColor:[UIColor blackColor]];
//二维码---2------
self.imgView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 350, 200, 200)];
[UIImageView productCodeWithImageView:self.imgView codeStr:@"12121"];
[self.view addSubview:_imgView];
// [self erweimaWithSize:_imgView.bounds.size.width codeStr:@"1212"];
}
@end
自定义的分类:
\#import <UIKit/UIKit.h>
@interface UIImageView (WProductCode)
//二维码
+ (void)productCodeWithImageView:(UIImageView *)imageV codeStr:(NSString *)codeStr;
@end
\#import "UIImageView+WProductCode.h"
@implementation UIImageView (WProductCode)
//二维码
+ (void)productCodeWithImageView:(UIImageView *)imageV codeStr:(NSString *)codeStr
{
//二维码滤镜
CIFilter *filter=[CIFilter filterWithName:@"CIQRCodeGenerator"];
//恢复滤镜的默认属性
[filter setDefaults];
//将字符串转换成NSData
NSData *data=[codeStr dataUsingEncoding:NSUTF8StringEncoding];
//通过KVO设置滤镜inputmessage数据
[filter setValue:data forKey:@"inputMessage"];
//获得滤镜输出的图像
CIImage *outputImage=[filter outputImage];
//将CIImage转换成UIImage,并放大显示
imageV.image=[self createNonInterpolatedUIImageFormCIImage:outputImage withSize:imageV.bounds.size.width];
//如果还想加上阴影,就在ImageView的Layer上使用下面代码添加阴影
imageV.layer.shadowOffset=CGSizeMake(0, 0.5);//设置阴影的偏移量
imageV.layer.shadowRadius=1;//设置阴影的半径
imageV.layer.shadowColor=[UIColor blackColor].CGColor;//设置阴影的颜色为黑色
imageV.layer.shadowOpacity=0.3;
}
+ (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 创建bitmap;
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 保存bitmap到图片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
@end
网友评论