Skip to content

Instantly share code, notes, and snippets.

@yarshure
Created December 12, 2016 08:45
Show Gist options
  • Select an option

  • Save yarshure/65a64ffcfbda0ec5af32db9cb4904679 to your computer and use it in GitHub Desktop.

Select an option

Save yarshure/65a64ffcfbda0ec5af32db9cb4904679 to your computer and use it in GitHub Desktop.
CIQRCodeGenerator CoreImage
let base64Encoded = proxy.base64String()
let stringData = base64Encoded.data(using: .utf8, allowLossyConversion: false)
let filter = CIFilter(name: "CIQRCodeGenerator")
guard let f = filter else {
return
}
f.setValue(stringData, forKey: "inputMessage")
f.setValue("M", forKey: "inputCorrectionLevel")
guard let image = f.outputImage else {
return
}
let cgImage = CIContext(options:nil).createCGImage(image, from:image.extent )
let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB();
let bytesPerRow = 2560
let info = CGBitmapInfo.byteOrder32Little.rawValue | CGImageAlphaInfo.premultipliedFirst.rawValue
let newContext:CGContext = CGContext(data: nil, width: 500 , height: 500, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace,bitmapInfo: info )!;
newContext.interpolationQuality = .none
newContext.draw(cgImage!, in: CGRect.init(x: 0, y: 0, width: 500, height: 500))
let newImage:CGImage = newContext.makeImage()!;
let nsImage = NSImage.init(cgImage:newImage, size: CGSize.init(width: 250 , height: 250))
self.imageView.image = nsImage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment