二维码的生成、保存指定二维码

http://www.cnblogs.com/gchlcc/p/5585482.html

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // 1.创建过滤器

    CIFilter * filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

    // 2.恢复默认

    [filter setDefaults];

    // 3.给过滤器添加数据

    NSString * imageUrl = @"http://mp.weixin.qq.com/s?__biz=MzI2MTA3OTE0NQ==&mid=504439213&idx=1&sn=5f81ffe341fea08719106483e709785e&scene=18#wechat_redirect";

    NSData * imageData = [imageUrl dataUsingEncoding:NSUTF8StringEncoding];

    // 4.通过KVO设置滤镜inputMessage数据

    [filter setValue:imageData forKeyPath:@"inputMessage"];

    // 4.获取输出的二维码

    CIImage * outputImage = [filter outputImage];

    // 5.CIImage转换成UIImage,并放大显示

    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, 280, 280)];

    [self.view addSubview:imageView];

    imageView.image =  [UIImage imageWithCIImage:outputImage scale:20.0 orientation:UIImageOrientationUp];

 

    

    

    UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];

    btn.frame = CGRectMake(100, 500, 30, 30);

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

    btn.backgroundColor = [UIColor redColor];

    [self.view addSubview:btn];

    

}

//截图按钮点击方法

-(void)photoClick{

    

    UIImageWriteToSavedPhotosAlbum([self screenView:self.view atFrame:CGRectMake(20, 100, 280, 280)], self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);

    

}

//将截取的图片返回方法

-(UIImage*)screenView:(UIView *)view atFrame:(CGRect)r{

    CGRect rect = [UIScreen mainScreen].bounds;

    

    

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    UIRectClip(r);

    [view.layer renderInContext:context];

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;

}

//实现imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:方法

- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

{

    

    if (error == nil) {

 

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"保存二维码图片"

                                                                       message:@"保存成功"

                                                                preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];

        [alert addAction:cancel];

        [self presentViewController:alert animated:YES completion:nil];

        

    }else{

        

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"保存二维码图片"

                                                                       message:@"保存成功"

                                                                preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];

        [alert addAction:cancel];

        [self presentViewController:alert animated:YES completion:nil];

    }

    

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

posted @ 2016-11-01 13:46  Ruby_c  阅读(366)  评论(0)    收藏  举报