是否有一种标准方法可以生成 QR 码并将其附加到来自 iOS 客户端应用程序的邮件项目(无服务器代码)?
问问题
38738 次
8 回答
96
从 iOS 7 开始,您可以使用 Core Image 过滤器来生成 QR 图像。请参阅此处的最终提示:
- (CIImage *)createQRForString:(NSString *)qrString {
NSData *stringData = [qrString dataUsingEncoding: NSISOLatin1StringEncoding];
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
return qrFilter.outputImage;
}
于 2014-03-10T09:35:21.103 回答
23
对于完美适合我的 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];
于 2016-06-22T11:48:49.843 回答
18
使用斯威夫特 2
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)
}
斯威夫特 3.0
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)
}
斯威夫特 4.2
private func createQRFromString(str: String) -> CIImage? {
let stringData = str.data(using: .utf8)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter?.setValue(stringData, forKey: "inputMessage")
filter?.setValue("H", forKey: "inputCorrectionLevel")
return filter?.outputImage
}
var qrCode: UIImage? {
if let img = createQRFromString(str: "Hello world program created by someone") {
let someImage = UIImage(
ciImage: img,
scale: 1.0,
orientation: UIImage.Orientation.down
)
return someImage
}
return nil
}
Xcode 12.4 或 Swift 版本 >= 5.2
import UIKit
import CoreImage
func createQRFromString(str: String) -> CIImage? {
let stringData = str.data(using: .utf8)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter?.setValue(stringData, forKey: "inputMessage")
filter?.setValue("H", forKey: "inputCorrectionLevel")
return filter?.outputImage
}
if let img = createQRFromString(str: "Hello world program created by someone") {
let somImage = UIImage(ciImage: img, scale: 1.0, orientation: .down)
}
于 2016-02-12T07:09:11.353 回答
14
自从提出这个问题以来已经有一段时间了,并且已经给出了一些几乎完美的答案。但是,我必须调整并结合几个答案,才能让它在 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
}
于 2018-01-05T09:08:30.533 回答
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)
于 2016-02-18T10:13:34.387 回答
0
如果您不介意使用公共 api,这里有一个简单的 10 秒方法。http://goqr.me/api
只需填写数据参数,然后从响应中加载图像。干杯。
于 2017-02-21T01:05:51.917 回答
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
于 2017-12-14T12:34:56.293 回答