QRコードを生成し、iOSクライアントアプリからメールアイテムに添付する標準的な方法はありますか(サーバーコードなし)?
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;
}
私にとって完璧に機能するObj-Cバージョンについては、answersと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];
Swift 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
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)
}
Swift 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
}
この質問が尋ねられてからしばらく経ち、多くのほぼ完璧な答えがすでに出されています。ただし、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
}
QR画像を生成するコードSwift 2.0。
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)
QR生成に QR Code Encoder を使用しました。使い方はとても簡単です。 this など、生成する他のオプションがいくつかあります。
Mike Demidovの素晴らしい答えを取り入れて、Obj-CのNSStringカテゴリを作成しました
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
パブリックAPIを使用してもかまわない場合は、10秒の簡単な方法があります。 http://goqr.me/api
データパラメータを入力し、応答から画像をロードするだけです。乾杯。