//首先创建一个控件,点击的时候到一下一界面,下一个界面生成二维码
MyCardViewController * card=[[MyCardViewController alloc]init];
//带http默认会直接跳到网站
// card.strCard=@"http://www.baidu.com";
//不会直接跳,需要点击
card.strCard=@"www.baidu.com";
[self.navigationController pushViewController:card animated:YES];
然后引入第三方库(libqrencode)的头文件
#import "QRCodeGenerator.h"//导入三方库头文件
//这个工程有个 字符串属性,用来接收从上个页面传过来的二维码数据。
//这个工程可以实现把二维码图片保存到手机的相册里
#import "MyCardViewController.h"
#import "QRCodeGenerator.h"//导入三方库头文件
@interface MyCardViewController ()
@end
@implementation MyCardViewController
{
UIImageView * _iView;//用来转换成二维码
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createUI];
[self updataData];
}
-(void)createUI
{
self.view.backgroundColor=[UIColor whiteColor];
_iView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
_iView.center=CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
[self.view addSubview:_iView];
UIButton * button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"save" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.frame=CGRectMake(0, _iView.frame.origin.y+_iView.frame.size.height+20, 50, 30);
button.center=CGPointMake(_iView.center.x, button.center.y);
[button addTarget:self action:@selector(saveImage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)updataData
{
//根据字符串产生二维码图片
UIImage * image=[QRCodeGenerator qrImageForString:_strCard imageSize:100];
_iView.image=image;
}
-(void)saveImage
{
//写到相册
UIImageWriteToSavedPhotosAlbum(_iView.image, self, @selector(image:didFinishSavingWithError:contextInfo:),NULL);
}
//指定的回调方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
NSString * message=nil;
if (error!=NULL) {
message=@"保存图片失败";
}else
{
message=@"保存图片成功";
}
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"保存图片结果提示" message:message delegate:nil cancelButtonTitle:@"1" otherButtonTitles:@"2", nil];
[alert show ];
}