Search code examples
objective-cxcodeqr-codearabicgenerate

objective c qr code won't generate when an arabic string is in it's data


My app has to generate a qr code which i succeeded in implementing using this block

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"], this is the code that include an arabic string once it's used then the qr code won't show up, no crash or any sign of failure.


Solution

  • 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];
    

    this code is working for me if anyone is having the same problem, i quoted it from an answer to a qr thread here on stackoverflow but it was voted once or twice so i didn't notice it.