#import "ViewController.h"
#import "UIImage+RoundedRectImage.h"
#import "KMQRCod.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imgVQRCode; //显示二维码部分
@property (weak, nonatomic) IBOutlet UITextField *textField; //输入内容
@property (nonatomic,retain) UIImage *saveImage; //将二维码图片接收
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark 确认按钮的点击事件
- (IBAction)confirmButtonClick:(UIButton *)sender {
if (self.textField.text.length) {
[self layoutUIWithString:self.textField.text];
} else {
NSLog(@"输入的内容不能为空");
}
}
#pragma mark 保存按钮点击事件
- (IBAction)saveClick:(UIButton *)sender {
if (self.saveImage) {
UIImageWriteToSavedPhotosAlbum(self.saveImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
}
//指定回调方法
-(void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo {
if(!error){
NSLog(@"保存成功");
}else{
NSLog(@"保存失败");
}
}
#pragma mark 生成二维码方法
/**
导入头文件
#import "UIImage+RoundedRectImage.h"
#import "KMQRCod.h"
*/
- (void)layoutUIWithString:(NSString *)str {
//用于生成二维码的字符串source
NSString *source = str;
//使用iOS 7后的CIFilter对象操作,生成二维码图片imgQRCode(会拉伸图片,比较模糊,效果不佳)
CIImage *imgQRCode = [KMQRCod createQRCodeImage:source];
//使用核心绘图框架CG(Core Graphics)对象操作,进一步针对大小生成二维码图片imgAdaptiveQRCode(图片大小适合,清晰,效果好)
UIImage *imgAdaptiveQRCode = [KMQRCod resizeQRCodeImage:imgQRCode
withSize:_imgVQRCode.frame.size.width];
//默认产生的黑白色的二维码图片;我们可以让它产生其它颜色的二维码图片,例如:蓝白色的二维码图片
imgAdaptiveQRCode = [KMQRCod specialColorImage:imgAdaptiveQRCode
withRed:33.0
green:114.0
blue:237.0]; //0~255
//使用核心绘图框架CG(Core Graphics)对象操作,创建带圆角效果的图片
UIImage *imgIcon = [UIImage createRoundedRectImage:[UIImage imageNamed:@"二维码"]
withSize:CGSizeMake(70.0, 70.0)
withRadius:10];
//使用核心绘图框架CG(Core Graphics)对象操作,合并二维码图片和用于中间显示的图标图片
imgAdaptiveQRCode = [KMQRCod addIconToQRCodeImage:imgAdaptiveQRCode
withIcon:imgIcon
withIconSize:imgIcon.size];
// imgAdaptiveQRCode = [KMQRCode addIconToQRCodeImage:imgAdaptiveQRCode
// withIcon:imgIcon
// withScale:3];
#pragma mark 获取保存的图片
self.saveImage = imgAdaptiveQRCode;
_imgVQRCode.image = imgAdaptiveQRCode;
//设置图片视图的圆角边框效果
_imgVQRCode.layer.masksToBounds = YES;
_imgVQRCode.layer.cornerRadius = 10.0;
_imgVQRCode.layer.borderColor = [UIColor lightGrayColor].CGColor;
_imgVQRCode.layer.borderWidth = 4.0;
}
#pragma mark 点击编辑区域以外的地方
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (![self.textField isExclusiveTouch]) {
[self.textField resignFirstResponder];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end