相关链接
#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];
}
#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 生成二维码方法
- (void)layoutUIWithString:(NSString *)str {
NSString *source = str;
CIImage *imgQRCode = [KMQRCod createQRCodeImage:source];
UIImage *imgAdaptiveQRCode = [KMQRCod resizeQRCodeImage:imgQRCode
withSize:_imgVQRCode.frame.size.width];
imgAdaptiveQRCode = [KMQRCod specialColorImage:imgAdaptiveQRCode
withRed:33.0
green:114.0
blue:237.0];
UIImage *imgIcon = [UIImage createRoundedRectImage:[UIImage imageNamed:@"二维码"]
withSize:CGSizeMake(70.0, 70.0)
withRadius:10];
imgAdaptiveQRCode = [KMQRCod addIconToQRCodeImage:imgAdaptiveQRCode
withIcon:imgIcon
withIconSize:imgIcon.size];
#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];
}
@end