导入第三方。 libqrencode
#import "QRCodeGenerator.h"
#import "OneViewController.h"
@interface ViewController ()
{
UIImageView *imgV;
UITextField *text;
UITextField*textF;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"二维码";
UIBarButtonItem*right = [[UIBarButtonItemalloc]initWithTitle:@"查看二维码"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(tiaoZhaun)];
self.navigationItem.rightBarButtonItem = right;
imgV = [[UIImageView alloc]initWithFrame:CGRectMake(100, 250, 200, 200)];
imgV.backgroundColor = [UIColor redColor];
[self.view addSubview:imgV];
// 设置按钮
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
// 设置背景颜色
btn.backgroundColor = [UIColor greenColor];
// 设置文字
[btnsetTitle:@"生成二维码" forState:UIControlStateNormal];
// 设置文字颜色
[btnsetTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 点击事件
[btnaddTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
// 添加到主界面
[self.viewaddSubview:btn];
}
// 跳转到下一个界面
- (void)tiaoZhaun{
[self presentViewController:[OneViewController new] animated:YES completion:nil];
}
// 点击事件
- (void)click{
// 创建提示框
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"创建二维码" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertsetAlertViewStyle:UIAlertViewStylePlainTextInput];
textF = [alerttextFieldAtIndex:0];
textF.placeholder=@"请输入字符串";
[alertshow];
}
- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex ==1) {
text= [alertViewtextFieldAtIndex:0];
}
// 生成二维码
UIImage *image = [QRCodeGenerator qrImageForString:text.text imageSize:imgV.frame.size.width];
imgV.image= image;
//保存二维码并给与二维码图片名称
NSString *filePath = [NSString stringWithFormat:@"%@/%@.png",NSHomeDirectory(),textF.text];
[UIImagePNGRepresentation(imgV.image) writeToFile:filePath atomically:YES];
}
//注册通知,点击点击进行通知的时候调用
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
UIApplication * application=[UIApplication sharedApplication];
//如果当前应用程序没有注册本地通知,需要注册
if([application currentUserNotificationSettings].types==UIUserNotificationTypeNone){
//设置提示支持的提示方式
// UIUserNotificationTypeBadge 提示图标
// UIUserNotificationTypeSound 提示声音
// UIUserNotificationTypeAlert 提示弹框
UIUserNotificationSettings *setting=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
[applicationregisterUserNotificationSettings:setting];
}
//删除之前的重复通知
[applicationcancelAllLocalNotifications];
//添加本地通知
NSDate * date=[NSDate dateWithTimeIntervalSinceNow:10];
[self LocalNotificationSleep :date];
}
#pragma mark - 添加本地通知
- (void) LocalNotificationSleep:(NSDate*) date{
UILocalNotification * noti=[[UILocalNotification alloc] init];
//设置开始时间
noti.fireDate=date;
//设置body
noti.alertBody=@"你有一条消息提醒,请查收!";
//设置action
noti.alertAction=@"详情";
//设置闹铃
noti.soundName=@"4195.mp3";
#warning 注册完之后如果不删除,下次会继续存在,即使从模拟器卸载掉也会保留
//注册通知
[[UIApplication sharedApplication] scheduleLocalNotification:noti];
}
下一个页面
@interface OneViewController ()
{
UIImageView *imgV;
}
@end
@implementationOneViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建图片框
imgV = [[UIImageView alloc]initWithFrame:CGRectMake(100, 250, 200, 200)];
// 设置背景颜色
imgV.backgroundColor = [UIColor redColor];
// 添加到主界面
[self.view addSubview:imgV];
NSString *filePath = [NSString stringWithFormat:@"%@/%@.png",NSHomeDirectory(),textF.text];
imgV.image = [UIImage imageWithContentsOfFile:filePath];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
[self dismissViewControllerAnimated:YES completion:nil];
}