根据内容生成二维码

2018-06-29  本文已影响0人  呵邢

1。拖拽控件

2.创建ShadowView,继承UIView

2.1.  .m中写入:

//引入头文件

#import

#define kWidth [UIScreen mainScreen].bounds.size.width

#define kHeight [UIScreen mainScreen].bounds.size.height

#define customShowSize CGSizeMake(200,200);

3.ViewController中

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextField *contentTF;

@property (weak, nonatomic) IBOutlet UIImageView *imgV;

- (IBAction)AnNiu:(id)sender;

@ end

@ implementation ViewController

-  (void) viewDidLoad {

    [super viewDidLoad];

}

-  (void) didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

- (IBAction)AnNiu:(id)sender {

    self.imgV.image=  [self createQRImageWithString:self.contentTF.text size:_imgV.frame.size];

}

- (UIImage*)createQRImageWithString:(NSString*)string size:(CGSize)size

{

    NSData  * stringData = [string dataUsingEncoding:NSUTF8StringEncoding];

    CIFilter * qrFilter = [CIFilterfilterWithName:@"CIQRCodeGenerator"];

    // NSLog(@"%@",qrFilter.inputKeys);

    [qrFilter setValue : stringData forKey:@"inputMessage"];

    [qrFilter setValue:@"M"  forKey:@"inputCorrectionLevel"];

    CIImage * qrImage = qrFilter.outputImage;

    //放大并绘制二维码 (上面生成的二维码很小,需要放大)

    CGImageRef cgImage = [[CIContext contextWithOptions : nil] createCGImage : qrImage fromRect : qrImage.extent];

    UIGraphicsBeginImageContext(size);

    CGContextRef  context = UIGraphicsGetCurrentContext();

    CGContextSetInterpolationQuality ( context , kCGInterpolationNone);

    //翻转一下图片 不然生成的QRCode就是上下颠倒的

    CGContextScaleCTM(context,1.0, -1.0);

    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);

    UIImage  * codeImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    CGImageRelease(cgImage);

    return codeImage;

}

@ end

*4.(保存二维码到沙盒)

//拿到图片

    UIImage*imagesave =self.imgV.image;

    NSString*path =NSHomeDirectory();

    //设置一个图片的存储路径

    NSString*imagePath = [pathstringByAppendingString:@"/Documents/test.png"];

    //把图片直接保存到指定的路径(同时应该把图片的路径imagePath存起来,下次就可以直接用来取)

    [UIImagePNGRepresentation(imagesave) writeToFile:imagePath atomically:YES];

*4.1 (二级界面查看沙盒二维码)

// 读取沙盒路径图片

    NSString *aPath3=[NSString stringWithFormat:@"%@/Documents/%@.png",NSHomeDirectory(),@"test"];

    // 拿到沙盒路径图片

    NSData*imgData = [[NSDataalloc]initWithContentsOfFile:aPath3];

    UIImage*imgFromUrl3=[[UIImagealloc]initWithData:imgData];

    self.imgVV.image= imgFromUrl3;

上一篇下一篇

猜你喜欢

热点阅读