【问题标题】:Sending an on-the-fly created QR Code UIImage by AirDrop fails通过 AirDrop 发送即时创建的 QR 码 UIImage 失败
【发布时间】:2016-11-05 22:36:08
【问题描述】:

我正在动态创建一个二维码并将其存储为 UIImage。现在我希望能够使用 UIActivityViewController 发送它,但不知何故它失败了:

func generateQRCode(from string: String) -> UIImage? {
    let data = string.data(using: String.Encoding.ascii)

    if let filter = CIFilter(name: "CIQRCodeGenerator") {
        filter.setValue(data, forKey: "inputMessage")
        let transform = CGAffineTransform(scaleX: 3, y: 3)

        if let output = filter.outputImage?.applying(transform) {
            return UIImage(ciImage: output)
        }
    }

    return nil
}

然后我调用该函数并将其存储为 UIImage:

let image = generateQRCode(from: "Create my Code")
imgQRCode.image = image

导出按钮使用以下动作:

@IBAction func shareButtonClicked(sender: UIButton) {

        let objectsToShare = [imgQRCode.image!] as [AnyObject]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

        activityVC.popoverPresentationController?.sourceView = sender
        self.present(activityVC, animated: true, completion: nil)

}

选择通过 AirDrop 发送到我的 Mac 后,应用程序崩溃

2016-11-05 23:29:15.912242 Ordnung[3945:888442] CGContextScaleCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

11 月 5 日 23:29:15 Ordnung[3945]:CGContextScaleCTM:无效上下文 0x0。如果要查看回溯,请设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量。

2016-11-05 23:29:15.912396 Ordnung[3945:888442] CGContextTranslateCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Nov  5 23:29:15  Ordnung[3945] <Error>: CGContextTranslateCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2016-11-05 23:29:15.912584 Ordnung[3945:888442] CGContextConcatCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Nov  5 23:29:15  Ordnung[3945] <Error>: CGContextConcatCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2016-11-05 23:29:15.912692 Ordnung[3945:888442] CGContextDrawImage: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Nov  5 23:29:15  Ordnung[3945] <Error>: CGContextDrawImage: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2016-11-05 23:29:15.927580 Ordnung[3945:888442] [AirDrop] Sender kSFOperationEventErrorOccured {
    Error = "Error Domain=SFOperation Code=-5 \"Die \U00dcbertragung ist fehlgeschlagen, da du eine ung\U00fcltige Datei senden wolltest.\" UserInfo={NSLocalizedDescription=Die \U00dcbertragung ist fehlgeschlagen, da du eine ung\U00fcltige Datei senden wolltest.}";
    SessionID = 76FBE80074FC;
}

有人知道如何完成这项工作吗?

干杯迈克

【问题讨论】:

    标签: ios swift swift3 qr-code uiactivityviewcontroller


    【解决方案1】:

    似乎我需要以稍微不同的方式提供图像,因此以这种方式更改 QR 码生成可以通过在创建 UIImage 之前创建 CGImage 来解决问题:

    func generateQRCode(from string: String) -> UIImage? {
        let ciContext = CIContext()
        let data = string.data(using: String.Encoding.ascii)
    
        if let filter = CIFilter(name: "CIQRCodeGenerator") {
            filter.setValue(data, forKey: "inputMessage")
            let transform = CGAffineTransform(scaleX: 3, y: 3)
            let upScaledImage = filter.outputImage?.applying(transform)
    
    
            let cgImage = ciContext.createCGImage(upScaledImage!,
                                                  from: upScaledImage!.extent)
            let qrcodeImage = UIImage(cgImage: cgImage!)
            return qrcodeImage
        }
        return nil
    }
    

    对于 Swift 4.2

    func generateQRCode(from string: String) -> UIImage? {
        let ciContext = CIContext()
        let data = string.data(using: String.Encoding.ascii)
    
        if let filter = CIFilter(name: "CIQRCodeGenerator") {
            filter.setValue(data, forKey: "inputMessage")
            let transform = CGAffineTransform(scaleX: 3, y: 3)
            let upScaledImage = filter.outputImage?.transformed(by: transform)
    
            let cgImage = ciContext.createCGImage(upScaledImage!, from: upScaledImage!.extent)
            let qrcodeImage = UIImage(cgImage: cgImage!)
            return qrcodeImage
        }
        return nil
    }
    

    此外,您应该使用 png 或 jpg 表示图像:

    func share() {
        let png = UIImagePNGRepresentation(qrcodeImage)
        let vc = UIActivityViewController(activityItems: [png!], applicationActivities: [])
        present(vc, animated: true)
    }
    

    【讨论】:

    • 你拯救了我的一天!谢谢!
    • 搜索了几天后,这是最终对我有用的解决方案。在所有其他解决方案中,您只能在屏幕上看到生成的图像,但无法将其保存为我尝试执行的文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2016-04-25
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多