Swift中的QR码阅读器和发生器

前端之家收集整理的这篇文章主要介绍了Swift中的QR码阅读器和发生器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
目前在Objective-C中,我使用Zbar( http://zbar.sourceforge.net/)生成&阅读QR码.现在,我想继续开发Swift开发,是否有关于在Swift中生成和读取QR代码的“如何”或库?
这是我写的用于在swift中生成QRcode的代码.
//MARK:- generate QR code
func generateQRImage(stringQR:NSString,withSizeRate rate:CGFloat) -> UIImage
{
    var filter:CIFilter = CIFilter(name:"CIQRCodeGenerator")
    filter.setDefaults()

    var data:NSData = stringQR.dataUsingEncoding(NSUTF8StringEncoding)!
    filter.setValue(data,forKey: "inputMessage")

    var outputImg:CIImage = filter.outputImage

    var context:CIContext = CIContext(options: nil)
    var cgimg:CGImageRef = context.createCGImage(outputImg,fromRect: outputImg.extent())

    var img:UIImage = UIImage(CGImage: cgimg,scale: 1.0,orientation: UIImageOrientation.Up)!

    var width  = img.size.width * rate
    var height = img.size.height * rate

    UIGraphicsBeginImageContext(CGSizeMake(width,height))
    var cgContxt:CGContextRef = UIGraphicsGetCurrentContext()
    CGContextSetInterpolationQuality(cgContxt,kCGInterpolationNone)
    img.drawInRect(CGRectMake(0,width,height))
    img = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return img
}
原文链接:https://www.f2er.com/swift/319213.html

猜你在找的Swift相关文章