2

I have the QR Code generator working, it display image to my qrImageView UIImageView.

Now, what I want to do is attach that QR Code image to email. Tried the code specified below but I get an error "found nil while unwrapping an Optional value".

Please help!

Function for generating QR CODE

  func generateQRCode(from string:String) -> UIImage? {

    let data =  string.data(using: String.Encoding.isoLatin1)

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

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

@IBAction func generateAction(_ sender: Any) {

    if let actualText = qrCodeTextField.text {
        let image = generateQRCode(from: actualText)
        qrImageView.image = image
    }



}

Code for attaching QR Code to Email

func configureMailController() -> MFMailComposeViewController {
    let mailComposerVC = MFMailComposeViewController()

    mailComposerVC.mailComposeDelegate = self

    mailComposerVC.setToRecipients(["tsm@yahoo.com"])
    mailComposerVC.setSubject("\(subject) QR Code")
    mailComposerVC.setMessageBody("Please see attached QR Code", isHTML: false)

    //Add Image as Attachment

    if let image = qrImageView.image {
        let data: NSData = UIImagePNGRepresentation(image)! as NSData

        mailComposerVC.addAttachmentData(data as Data, mimeType: "image/png", fileName: "image")

    }


    return mailComposerVC

}
TSM
  • 1,225
  • 1
  • 9
  • 17

2 Answers2

7

This works for me. I have to convert the CIImage first to UIImage so that the QRCode can be attached to email

func convert(cmage:CIImage) -> UIImage
{       
    let context:CIContext = CIContext.init(options: nil)
    let cgImage:CGImage = context.createCGImage(cmage, from: cmage.extent)!        
    let image:UIImage = UIImage.init(cgImage: cgImage)        
    return image
}

Source: Unable to convert CIImage to UIImage in Swift 3.0

TSM
  • 1,225
  • 1
  • 9
  • 17
0

Try this,

if let image = qrImageView.image { 
    let data = UIImagePNGRepresentation(image) as NSData?
}

Instead of

if let image = qrImageView.image {
    let data = UIImagePNGRepresentation(image)! as NSData
}

It seems that this is the only part of the code that force unwraps the value.

Kiester
  • 147
  • 1
  • 7
  • Hello. Thanks for your reply. I tried it and i still get the same error. It seems that it can't find any value inside the qrImageView.image even if it is currently displaying the QRCode image. – TSM Aug 04 '17 at 13:48
  • I believe this is the line of code that throws an error. It occurs whenever I click on the send email button. I actually tried this code *** if let image = qrImageView.image { if let data = UIImagePNGRepresentation(image) { mailComposerVC.addAttachmentData(data, mimeType: "image/png", fileName: "image") } } *** and the email page shows up but it does not contain any attachment, only the subject, recipient and message body – TSM Aug 04 '17 at 14:01