0

我的应用程序必须生成一个 qr 代码,我使用此块成功实现了该代码

NSString *info = [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@", [_sighting valueForKey:@"Sighting_id"], [_sighting valueForKey:@"Name"], [_sighting valueForKey:@"Type"], [_sighting valueForKey:@"Distance"], [_sighting valueForKey:@"Loc"], session.userId];
NSLog(@"%@", info);
NSData *qrCodeData = [info dataUsingEncoding:NSISOLatin1StringEncoding];
CIFilter *qrCodeFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrCodeFilter setValue:qrCodeData forKey:@"inputMessage"];
[qrCodeFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
CIImage *qrCodeImage = qrCodeFilter.outputImage;
CGRect imageSize = CGRectIntegral(qrCodeImage.extent); 
CGSize outputSize = CGSizeMake(240.0, 240.0);
CIImage *imageByTransform = [qrCodeImage imageByApplyingTransform:CGAffineTransformMakeScale(outputSize.width/CGRectGetWidth(imageSize), outputSize.height/CGRectGetHeight(imageSize))];
UIImage *qrCodeImageByTransform = [UIImage imageWithCIImage:imageByTransform];
self.qrimage.image = qrCodeImageByTransform;

[_sighting valueForKey:@"Name"],这是包含阿拉伯字符串的代码,一旦使用,二维码将不会显示,不会出现崩溃或任何失败迹象。

4

1 回答 1

0
NSString *info = [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@", [_sighting valueForKey:@"Sighting_id"], [_sighting valueForKey:@"Name"], [_sighting valueForKey:@"Type"], [_sighting valueForKey:@"Distance"], [_sighting valueForKey:@"Loc"], session.userId];

NSData *stringData = [info dataUsingEncoding: NSUTF8StringEncoding];

CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];

CIImage *qrImagenew = qrFilter.outputImage;
float scaleX = self.qrimage.frame.size.width / qrImagenew.extent.size.width;
float scaleY = self.qrimage.frame.size.height / qrImagenew.extent.size.height;

qrImagenew = [qrImagenew imageByApplyingTransform:CGAffineTransformMakeScale(scaleX, scaleY)];

self.qrimage.image = [UIImage imageWithCIImage:qrImagenew
                                             scale:[UIScreen mainScreen].scale
                                       orientation:UIImageOrientationUp];

如果有人遇到同样的问题,这段代码对我有用,我在stackoverflow上从一个qr线程的答案中引用了它,但它被投票了一两次,所以我没有注意到它。

于 2018-03-06T12:41:08.270 回答