11

I am trying to convert a UIImage to a SwiftUI Image using the init(uiImage:) initializer. My UIImage itself is created from a CIImage generated by a CIQRCodeGenerator CIFilter. I am running my code on a Playground in Xcode 11.1 GM seed 1. Here is the entirety of my code:

import SwiftUI
import UIKit

func qrCodeImage(for string: String) -> Image? {
  let data = string.data(using: String.Encoding.utf8)
  guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
  qrFilter.setValue(data, forKey: "inputMessage")

  guard let ciImage = qrFilter.outputImage else { return nil }
  let uiImage = UIImage(ciImage: ciImage)
  let image = Image(uiImage: uiImage)

  return image
}

let image = qrCodeImage(for: "fdsa")

And here is the result:

enter image description here

Even when I transform the image with CGAffineTransform(scaleX: 10, y: 10), the resulting SwiftUI Image at the end is still the same size, but blank.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
Eugene
  • 3,417
  • 5
  • 25
  • 49
  • Does any of this help? - https://stackoverflow.com/questions/32297704/convert-uiimage-to-nsdata-and-convert-back-to-uiimage-in-swift – gotnull Sep 25 '19 at 05:30
  • 1
    I don't think it helps. That question is from 2015. SwiftUI was unveiled in 2019. My question is about converting a UIImage to a SwiftUI Image. – Eugene Sep 25 '19 at 14:06
  • 2
    Encounter same problem, but resolved it using solution from: https://stackoverflow.com/questions/58239980/generating-qr-code-with-swiftui-shows-empty-picture – wk.experimental Oct 25 '19 at 08:55
  • Does this answer your question? [Generating QR Code with SwiftUI shows empty picture](https://stackoverflow.com/questions/58239980/generating-qr-code-with-swiftui-shows-empty-picture) – jtbandes Feb 10 '23 at 07:08

3 Answers3

15

Following solution provided in: Generating QR Code with SwiftUI shows empty picture

Here is the code:

var ciContext = CIContext()

func qrCodeImage(for string: String) -> Image? {
    let data = string.data(using: String.Encoding.utf8)
    guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
    qrFilter.setValue(data, forKey: "inputMessage")

    guard let ciImage = qrFilter.outputImage else { return nil }
    let cgImage = ciContext.createCGImage(ciImage, from: ciImage.extent)

    let uiImage = UIImage(cgImage: cgImage!)

    let image = Image(uiImage: uiImage)

    return image
}

 let image = qrCodeImage(for: "fdsa")

Result:

screenshot in swift playground

wk.experimental
  • 523
  • 4
  • 9
5

Can confirm I encounter the same issue with a SwiftUI Image using a UIImage initialized from data. Can verify that the image is loaded when paused in debugging, but it does not display in the SwiftUI Image.

This solution worked for me: explicitly specify the image rendering mode. In my case I added the following: .renderingMode(.original)

gotnull
  • 26,454
  • 22
  • 137
  • 203
  • 1
    Have you tried this on my playground example? I changed `let image = Image(uiImage: uiImage)` to `let image = Image(uiImage: uiImage).renderingMode(.original)` but I got the same result. – Eugene Sep 27 '19 at 01:58
  • No, I did not. Here is the full declaration I use. Only adding the renderingMode worked in my case, but maybe there is also a dependency on the other values: `Image(uiImage: photo.thumbnailImage!).renderingMode(.original).resizable().aspectRatio(contentMode: .fill).frame(width: 50, height: 50).cornerRadius(25)` – David Earnest Sep 27 '19 at 20:48
0

@Eugene remark worked for me:

let image = Image(uiImage: uiImage).renderingMode(.original)
Shaybc
  • 2,628
  • 29
  • 43