是否有标准的方法来生成QR码并将其附加到iOS客户端应用程序中的邮件项目(无服务器代码)?
答案 0 :(得分:83)
从iOS 7开始,您可以使用Core Image过滤器生成QR图像。见the final tip here:
- (CIImage *)createQRForString:(NSString *)qrString {
NSData *stringData = [qrString dataUsingEncoding: NSISOLatin1StringEncoding];
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
return qrFilter.outputImage;
}
答案 1 :(得分:16)
对于对我来说非常有用的Obj-C版本,我混合了答案和Teja Kumar Bethina:
NSString *qrString = @"My string to encode";
NSData *stringData = [qrString dataUsingEncoding: NSUTF8StringEncoding];
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];
CIImage *qrImage = qrFilter.outputImage;
float scaleX = self.qrImageView.frame.size.width / qrImage.extent.size.width;
float scaleY = self.qrImageView.frame.size.height / qrImage.extent.size.height;
qrImage = [qrImage imageByApplyingTransform:CGAffineTransformMakeScale(scaleX, scaleY)];
self.qrImageView.image = [UIImage imageWithCIImage:qrImage
scale:[UIScreen mainScreen].scale
orientation:UIImageOrientationUp];
答案 2 :(得分:13)
使用Swift 2
canvas {
border: 1px solid #000000;
position: relative;
left: 500px; (Play with this # to position horizontally)
}
Swift 3.0
import UIKit
import CoreImage
func createQRFromString(str: String) -> CIImage? {
let stringData = str.dataUsingEncoding(NSUTF8StringEncoding)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter?.setValue(stringData, forKey: "inputMessage")
filter?.setValue("H", forKey: "inputCorrectionLevel")
return filter?.outputImage
}
if let img = createQRFromString("Hello world program created by someone") {
let somImage = UIImage(CIImage: img, scale: 1.0, orientation: UIImageOrientation.Down)
}
Swift 4.2
import UIKit
import CoreImage
func createQRFromString(_ str: String) -> CIImage? {
let stringData = str.data(using: String.Encoding.utf8)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter?.setValue(stringData, forKey: "inputMessage")
filter?.setValue("H", forKey: "inputCorrectionLevel")
if let img = createQRFromString("Hello world program created by someone") {
let somImage = UIImage(ciImage: img, scale: 1.0, orientation: UIImageOrientation.down)
}
return filter?.outputImage
}
if let img = createQRFromString("Hello world program created by someone") {
let somImage = UIImage(ciImage: img, scale: 1.0, orientation: UIImageOrientation.down)
}
答案 3 :(得分:10)
自问这个问题以来已经有一段时间了,并且已经给出了一些近乎完美的答案。然而,我不得不调整并结合几个答案,以便在2018年使用Xcode 9.2对AppleTV 4K,iPhone X和iPadPro进行完美的工作。如果有人需要,可以使用代码。
@IBOutlet weak var qrCodeBox: UIImageView!
func createQRFromString(_ str: String, size: CGSize) -> UIImage {
let stringData = str.data(using: .utf8)
let qrFilter = CIFilter(name: "CIQRCodeGenerator")!
qrFilter.setValue(stringData, forKey: "inputMessage")
qrFilter.setValue("H", forKey: "inputCorrectionLevel")
let minimalQRimage = qrFilter.outputImage!
// NOTE that a QR code is always square, so minimalQRimage..width === .height
let minimalSideLength = minimalQRimage.extent.width
let smallestOutputExtent = (size.width < size.height) ? size.width : size.height
let scaleFactor = smallestOutputExtent / minimalSideLength
let scaledImage = minimalQRimage.transformed(
by: CGAffineTransform(scaleX: scaleFactor, y: scaleFactor))
return UIImage(ciImage: scaledImage,
scale: UIScreen.main.scale,
orientation: .up)
}
override func viewDidLoad() {
super.viewDidLoad()
let myQRimage = createQRFromString("https://www.apple.com",
size: qrCodeBox.frame.size)
qrCodeBox.image = myQRimage
}
答案 4 :(得分:4)
在Swift 2.0中生成QR图像的代码。
let reqStr = “string to convert as QR code”
let data = reqStr.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: false)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter!.setValue(data, forKey: "inputMessage")
let qrImage:CIImage = filter!.outputImage!
//qrImageView is a IBOutlet of UIImageView
let scaleX = qrImageView.frame.size.width / qrImage.extent.size.width
let scaleY = qrImageView.frame.size.height / qrImage.extent.size.height
let resultQrImage = qrImage.imageByApplyingTransform(CGAffineTransformMakeScale(scaleX, scaleY))
qrImageView.image = UIImage(CIImage: resultQrImage)
答案 5 :(得分:0)
我已使用QR Code Encoder进行QR生成。这是非常容易使用。还有一些其他选项可以生成,例如this。
答案 6 :(得分:0)
...这将让我生成链接...
首先,您需要找到一个短链接服务,例如bit.ly或goo.by,可缩短长链接,从而减少QR码的大小。
要自动执行此任务,您必须使用某些Web服务。
...然后生成QR码......
ZXing是一种流行的开源QR码生成器,也适用于iOS
答案 7 :(得分:0)
如果您不介意使用公共API,这是一个简单的10秒方法。 http://goqr.me/api
只需填写数据参数,然后从响应中加载图像。欢呼声。
答案 8 :(得分:0)
我为Obj-C创建了一个NSString类别,采用了Mike Demidov非常棒的答案
<强> NString + GGQRCode.h 强>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface NSString (GGQRCode)
-(UIImage *)qrCodeImage:(CGFloat)width height:(CGFloat)height;
-(UIImage *)qrCodeImage:(CGFloat)width height:(CGFloat)height scale:(CGFloat)scale orientation:(UIImageOrientation)orientation;
@end
<强> NString + GGQRCode.m 强>
#import "NSString+GGQRCode.h"
@implementation NSString (GGQRCode)
-(UIImage *)qrCodeImage:(CGFloat)width height:(CGFloat)height
{
return [self qrCodeImage:width height:height scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
}
-(UIImage *)qrCodeImage:(CGFloat)width height:(CGFloat)height scale:(CGFloat)scale orientation:(UIImageOrientation)orientation
{
NSData *stringData = [self dataUsingEncoding: NSUTF8StringEncoding];
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];
CIImage *qrImage = qrFilter.outputImage;
float scaleX = width / qrImage.extent.size.width;
float scaleY = height / qrImage.extent.size.height;
qrImage = [qrImage imageByApplyingTransform:CGAffineTransformMakeScale(scaleX, scaleY)];
return [UIImage imageWithCIImage:qrImage scale:scale orientation:orientation];
}
@end