- 今天被后臺(tái)要求在前端自己生成一個(gè)二維碼圖片,并且分享這張圖片,之前沒搞過,于是開始百度,發(fā)現(xiàn)一個(gè)第三方庫叫
libqrencode看了一下demo感覺很容易,于是感覺往工程里版 用CocoaPods搜索了一下pod search libqrencode結(jié)果還真有pod 'libqrencode', '~> 3.4.2 '于是復(fù)制這行信息到 工程Pods目錄下的Podfile文件中 ,pod install,運(yùn)行工程,結(jié)果沒有效果,原因是libqrencode這個(gè)庫不支持生成圖片,繼續(xù)百度 發(fā)現(xiàn)還有一個(gè)類別人封裝的 (我沒本事封裝)叫QRCodeGenerator名字隨意取 里面的內(nèi)容才是重要的,把這個(gè)類導(dǎo)入之后 效果立竿見影! - 上代碼
QRCodeGenerator.h:
#import <Foundation/Foundation.h>
@interface QRCodeGenerator : NSObject
+ (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)size;
@end
QRCodeGenerator.m:
#import "QRCodeGenerator.h"
#import "qrencode.h"
enum {
qr_margin = 3
};
@implementation QRCodeGenerator
+ (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size {
unsigned char *data = 0;
int width;
data = code->data;
width = code->width;
float zoom = (double)size / (code->width + 2.0 * qr_margin);
CGRect rectDraw = CGRectMake(0, 0, zoom, zoom);
// draw
CGContextSetFillColor(ctx, CGColorGetComponents([UIColor blackColor].CGColor));
for(int i = 0; i < width; ++i) {
for(int j = 0; j < width; ++j) {
if(*data & 1) {
rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom);
CGContextAddRect(ctx, rectDraw);
}
++data;
}
}
CGContextFillPath(ctx);
}
+ (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)size {
if (![string length]) {
return nil;
}
QRcode *code = QRcode_encodeString([string UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1);
if (!code) {
return nil;
}
// create context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, kCGImageAlphaPremultipliedLast);
CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size);
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1);
CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform));
// draw QR on this context
[QRCodeGenerator drawQRCode:code context:ctx size:size];
// get image
CGImageRef qrCGImage = CGBitmapContextCreateImage(ctx);
UIImage * qrImage = [UIImage imageWithCGImage:qrCGImage];
// some releases
CGContextRelease(ctx);
CGImageRelease(qrCGImage);
CGColorSpaceRelease(colorSpace);
QRcode_free(code);
return qrImage;
}
@end
重點(diǎn)已介紹完畢,怎么讓他顯示呢? 繼續(xù)
- 創(chuàng)建一個(gè)UIImageView 用來顯示生成的二維碼圖片 我的這個(gè)UIImageView 創(chuàng)建在MyViewController.m的viewDidLoad里面,是不是有點(diǎn)啰嗦。。??创a。
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView * imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 300, 300)] autorelease];
imageView.image = [QRCodeGenerator qrImageForString:@"我這個(gè)字符串填的是圖片鏈接" imageSize:imageView.bounds.size.width];
[self.view addSubview:imageView];
}
- 二維碼圖片已經(jīng)可以顯示了,但是還有一個(gè)問題,怎么把他分享出去呢? PS 我分享用的shareSDK 思考了半天,怎么拿到二維碼圖片的名字呢,絕對(duì)走投無路,于是去看shareSDK的.h文件,結(jié)果欣喜如狂找到。
+ (id<ISSCAttachment>)imageWithPath:(NSString *)path;文檔默認(rèn)方法
+ (id<ISSCAttachment>)imageWithUrl:(NSString *)url;http開頭的圖片鏈接
+ (id<ISSCAttachment>)jpegImageWithImage:(UIImage *)image quality:(CGFloat)quality;自定義圖片質(zhì)量的
+ (id<ISSCAttachment>)pngImageWithImage:(UIImage *)image;(就是我想要的,可以直接把UIiamge分享出去)
- end 介紹完畢