iOS8 生成二维码与条形码
iOS8 生成二维码与条形码
效果图:
源码:
// // ViewController.m // CodeCreator // // Created by YouXianMing on 15/3/16. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIImageView *imageView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; self.imageView.center = self.view.center; [self.view addSubview:self.imageView]; [self generateCode]; } - (UIImage *)resizeImage:(UIImage *)image withQuality:(CGInterpolationQuality)quality rate:(CGFloat)rate { UIImage *resized = nil; CGFloat width = image.size.width * rate; CGFloat height = image.size.height * rate; UIGraphicsBeginImageContext(CGSizeMake(width, height)); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetInterpolationQuality(context, quality); [image drawInRect:CGRectMake(0, 0, width, height)]; resized = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resized; } - (void)generateCode { // @"CICode128BarcodeGenerator" 条形码 // @"CIAztecCodeGenerator" 二维码 NSString *filtername = @"CICode128BarcodeGenerator"; CIFilter *filter = [CIFilter filterWithName:filtername]; [filter setDefaults]; NSData *data = [@"YouXianMing" dataUsingEncoding:NSUTF8StringEncoding]; [filter setValue:data forKey:@"inputMessage"]; CIImage *outputImage = [filter outputImage]; CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef cgImage = [context createCGImage:outputImage fromRect:[outputImage extent]]; UIImage *image = [UIImage imageWithCGImage:cgImage scale:1. orientation:UIImageOrientationUp]; // Resize without interpolating CGFloat scaleRate = self.imageView.frame.size.width / image.size.width; UIImage *resized = [self resizeImage:image withQuality:kCGInterpolationNone rate:scaleRate]; self.imageView.image = resized; CGImageRelease(cgImage); } @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。