iOS QR코드 작성(Swift3.0)

1352 단어
import UIKit  
  
public extension UIImage {  
      
    public class func createQRCode(code: String, width: CGFloat, height: CGFloat) -> UIImage? {  
        let data = code.data(using: String.Encoding.isoLatin1, allowLossyConversion: false)  
        if let filter = CIFilter(name: "CIQRCodeGenerator") {  
            filter.setValue(data, forKey: "inputMessage")  
            filter.setValue("H", forKey: "inputCorrectionLevel")  
//            inputCorrectionLevel       (@"L", @"M", @"Q", @"H"     ),          ,    @"M"  
//                   L   7%         
//            M   15%         
//            Q   25%         
//            H   30%         
//                                                
            if let QRCodeImage = filter.outputImage {  
                //      
                let scaleX = width/QRCodeImage.extent.size.width  
                let scaleY = height/QRCodeImage.extent.size.height  
                let transformedImage = QRCodeImage.applying(CGAffineTransform.init(scaleX: scaleX, y: scaleY))  
                  
                return UIImage(ciImage: transformedImage)  
            } else {  
                return nil  
            }  
        }else {  
            return nil  
        }  
    }  
}

좋은 웹페이지 즐겨찾기