要在 iOS 中生成二維碼,首先需要下載二維碼生成的支持庫 libqrencode,並添加以下依賴庫:CoreGraphics.framework、QuartzCore.framework、AVFoundation.framework、libiconv.dylib、CoreMedia.framework、CoreVideo.framework。
在需要生成二維碼的界面文件中添加 #import "QRCodeGenerator.h",然後在你放置二維碼的 ImageView 上,一行代碼就能搞定:
imageV.image = [QRCodeGenerator qrImageForString:@"這是生成的二維碼哦" imageSize:imageV.bounds.size.width]
注意事項:如果你的工程裡寫有 .pch 文件,請把支持的頭文件寫成只讓 OC 類引用的格式
例如下面這樣:
#ifndef PrefixHeader_pch #define PrefixHeader_pch #ifdef __OBJC__ #import "xxxxx.h" #import "xxxxx.h" #endif
這樣就可以了。