美文网首页
iOS 生成二维码

iOS 生成二维码

作者: 财奴 | 来源:发表于2018-12-07 20:47 被阅读0次

    //

    //  scewmViewController.m

    //  练习1

    //

    //  Created by 与你共度 on 2018/12/20.

    //  Copyright © 2018 与你共度. All rights reserved.

    //

    #import "scewmViewController.h"

    #import "QRCodeGenerator.h"

    @interface scewmViewController ()

    {

        UITextField*field;

        UIButton*btn;

        UIImageView*imgVV;

    }

    - (IBAction)Back:(id)sender;

    @end

    @implementationscewmViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        self.view.backgroundColor = [UIColor whiteColor];

        //文本框的位置

        field= [[UITextFieldalloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2,100,200,44)];

        //提示文字

        field.placeholder = @"请输入内容";

        field.borderStyle = UITextBorderStyleRoundedRect;

        //添加到视图

        [self.view addSubview:field];

        //按钮的位置

        btn= [[UIButtonalloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2,180,200,44)];

        //为按钮设置文字

        [btn setTitle:@"点击生成二维码" forState:UIControlStateNormal];

        //为按钮设置背景颜色

        btn.backgroundColor = [UIColor lightGrayColor];

        //给按钮添加一个点击方法

        [btn addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:btn];

    }

    -(void)abc{

        //    设置UIImageView的位置

        imgVV= [[UIImageViewalloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2,300,200,200)];

        //为UIImageView设置二维码图片

        imgVV.image = [QRCodeGenerator qrImageForString:field.text imageSize:imgVV.bounds.size.width];

        //把图片添加到视图

        [self.view addSubview:imgVV];

        // Do any additional setup after loading the view.

    }

    /*

     #pragma mark - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation

     - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

     // Get the new view controller using [segue destinationViewController].

     // Pass the selected object to the new view controller.

     }

     */

    - (IBAction)Back:(id)sender {

        [self dismissViewControllerAnimated:YES completion:nil];

    }

    @end

    相关文章

      网友评论

          本文标题:iOS 生成二维码

          本文链接:https://www.haomeiwen.com/subject/dlbxhqtx.html