Is there a standard way to generate a QR code and attach it to a mail item from iOS client app (no server code)?
是否有一种标准的方法来生成二维码并将其附加到iOS客户端应用程序的邮件条目上(没有服务器代码)?
9 个解决方案
#1
75
Since iOS 7, you can use a Core Image filter to generate QR images. See the final tip here:
自ios7以来,您可以使用核心图像过滤器来生成QR图像。请看最后的提示:
- (CIImage *)createQRForString:(NSString *)qrString {
NSData *stringData = [qrString dataUsingEncoding: NSISOLatin1StringEncoding];
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
return qrFilter.outputImage;
}
#2
12
For Obj-C version that perfectly works for me, I've mixed answers पवन and Teja Kumar Bethina:
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];
#3
12
Using Swift 2
使用快速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)
}
Swift 3.0
斯威夫特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
5
It has been a while since this question was asked and a number of almost perfect answers have been given already. However I had to tweak and combine several answers to get it working perfectly for AppleTV 4K, iPhone X and iPadPro using Xcode 9.2 in 2018. Here's the code if anyone needs it.
问这个问题已经有一段时间了,现在已经给出了一些近乎完美的答案。然而,我不得不调整和合并几个答案,以便在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
}
#5
3
Code to generate QR image in Swift 2.0.
在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)
#6
0
I have used the QR Code Encoder for QR generation. It is very easy to use. There are some other option to generate, e.g this.
我使用了二维码编码器进行二维码生成。它很容易使用。还有其他生成选项,e。g。
#7
0
...which will let me generate links...
…这会让我生成链接……
First, you need to find a short link service, e.g. bit.ly or goo.by, to make a long link shorter and thus reduce QR Code size.
首先,您需要找到一个短链接服务,例如bit。ly或感伤。通过,使长链接更短,从而减少二维码的大小。
To perform this task automatically you will have to use some web service.
要自动执行此任务,您必须使用一些web服务。
...then generate a QR code...
…然后生成二维码……
ZXing is a popular open source QR Code generator also available for iOS
ZXing是一个流行的开源二维码生成器,iOS也可以使用它
#8
0
If you don't mind using public api, here is an easy 10 seconds method. http://goqr.me/api
如果您不介意使用公共api,这里有一个简单的10秒方法。http://goqr.me/api
Just fill in the data parameter, and load the image from the response. Cheers.
只需填充数据参数,并从响应加载图像。欢呼。
#9
0
I created a NSString Category for Obj-C, taking Mike Demidov awesome answer
我为object - c创建了一个NSString类别,选了Mike Demidov的超棒答案
NString+GGQRCode.h
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
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
#1
75
Since iOS 7, you can use a Core Image filter to generate QR images. See the final tip here:
自ios7以来,您可以使用核心图像过滤器来生成QR图像。请看最后的提示:
- (CIImage *)createQRForString:(NSString *)qrString {
NSData *stringData = [qrString dataUsingEncoding: NSISOLatin1StringEncoding];
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
return qrFilter.outputImage;
}
#2
12
For Obj-C version that perfectly works for me, I've mixed answers पवन and Teja Kumar Bethina:
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];
#3
12
Using Swift 2
使用快速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)
}
Swift 3.0
斯威夫特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
5
It has been a while since this question was asked and a number of almost perfect answers have been given already. However I had to tweak and combine several answers to get it working perfectly for AppleTV 4K, iPhone X and iPadPro using Xcode 9.2 in 2018. Here's the code if anyone needs it.
问这个问题已经有一段时间了,现在已经给出了一些近乎完美的答案。然而,我不得不调整和合并几个答案,以便在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
}
#5
3
Code to generate QR image in Swift 2.0.
在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)
#6
0
I have used the QR Code Encoder for QR generation. It is very easy to use. There are some other option to generate, e.g this.
我使用了二维码编码器进行二维码生成。它很容易使用。还有其他生成选项,e。g。
#7
0
...which will let me generate links...
…这会让我生成链接……
First, you need to find a short link service, e.g. bit.ly or goo.by, to make a long link shorter and thus reduce QR Code size.
首先,您需要找到一个短链接服务,例如bit。ly或感伤。通过,使长链接更短,从而减少二维码的大小。
To perform this task automatically you will have to use some web service.
要自动执行此任务,您必须使用一些web服务。
...then generate a QR code...
…然后生成二维码……
ZXing is a popular open source QR Code generator also available for iOS
ZXing是一个流行的开源二维码生成器,iOS也可以使用它
#8
0
If you don't mind using public api, here is an easy 10 seconds method. http://goqr.me/api
如果您不介意使用公共api,这里有一个简单的10秒方法。http://goqr.me/api
Just fill in the data parameter, and load the image from the response. Cheers.
只需填充数据参数,并从响应加载图像。欢呼。
#9
0
I created a NSString Category for Obj-C, taking Mike Demidov awesome answer
我为object - c创建了一个NSString类别,选了Mike Demidov的超棒答案
NString+GGQRCode.h
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
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