iOS 7 Core Image QR Code generation too blur

前端 未结 8 1581
心在旅途
心在旅途 2020-12-24 02:44

here\'s my code for generating QRCode image

+ (UIImage *)generateQRCodeWithString:(NSString *)string {
    NSData *stringData = [string dataUsingEncoding:NSU         


        
8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 03:26

    I had to adapt @cromanelli's answer to achieve perfect sharpness :

    func convertTextToQRCode(text: String, withSize size: CGSize) -> UIImage {
    
        let data = text.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: false)
    
        let filter = CIFilter(name: "CIQRCodeGenerator")!
    
        filter.setValue(data, forKey: "inputMessage")
        filter.setValue("L", forKey: "inputCorrectionLevel")
    
        var qrcodeCIImage = filter.outputImage!
    
        let cgImage = CIContext(options:nil).createCGImage(qrcodeCIImage, fromRect: qrcodeCIImage.extent)
        UIGraphicsBeginImageContext(CGSizeMake(size.width * UIScreen.mainScreen().scale, size.height * UIScreen.mainScreen().scale))
        let context = UIGraphicsGetCurrentContext()
        CGContextSetInterpolationQuality(context, .None)
        CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage)
        let preImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
        let qrCodeImage = UIImage(CGImage: preImage.CGImage!, scale: 1.0/UIScreen.mainScreen().scale, orientation: .DownMirrored)
    
        return qrCodeImage
    }
    

提交回复
热议问题