Reputation: 15512
I am updating existing app to support iOS 12 and strange issue with CIFilter
appears.
This is simple class with one function generate()
:
class QRGenerator {
static func generate(from string: String) -> UIImage? {
let context = CIContext()
let data = string.data(using: String.Encoding.ascii)
if let filter = CIFilter(name: "CIQRCodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 7, y: 7)
if let output = filter.outputImage?.transformed(by: transform), let cgImage = context.createCGImage(output, from: output.extent) {
return UIImage(cgImage: cgImage)
}
}
return nil
}
}
This class work perfectly until iOS 12. Now in line if let filter = CIFilter(name: "CIQRCodeGenerator")
I always receive nil.
I have spend some time on the Apple documentation but do not find any useful information about this issue.
Upvotes: 5
Views: 3739
Reputation: 114
In Swift 5
class QRGenerator {
static func generate(from string: String) -> UIImage? {
let data = string.data(using: .ascii, allowLossyConversion: false)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter?.setValue(data, forKey: "inputMessage")
return UIImage(ciImage: (filter?.outputImage)!)
}
}
call this function
myImageView.image = QRGenerator.generate(from: "Stackoverflow")
Upvotes: 1
Reputation: 1932
It happens to me too, I also tried generating the QR with an external library like QRcode https://github.com/aschuch/QRCode but the image of the QR is always nil.
It's nil when I install the app in emulators with iOS 12.0 (16A5308d) from Xcode 10.0 beta 2.
But when I install it in a physical device iPhone SE with iOS 12 beta 4 (16A5339e) from the same Xcode (10.0 beta 2), the QR is generated with no problem at all.
I also tried with an emulated iPhone SE with iOS 11.4 (15F79) from the same Xcode (10.0 beta 2) and works perfect.
Upvotes: 1
Reputation: 17735
I know very little about CIFilters
, but may be you could try to get all possible filter names:
let allFiltersNames = CIFilter.filterNames(inCategories: nil)
I couldn't spot one that matches CIQRCodeGenerator
but may be there is some other filter with a different name that would meet your needs.
Upvotes: 0