Swift : No Matter what I do CIDetector is always nil

*爱你&永不变心* 提交于 2019-12-18 18:23:11

问题


i don't understand why this code doesn't work, the detector is always nil with the CIDetectorTypeQRCode constant, everything work with CIDetectorTypeFace. I Supect a bug from API of Apple. This a the official doc : Apple documentation

@IBAction func analyseTag(sender: AnyObject) {

        var detector:CIDetector = CIDetector(ofType: CIDetectorTypeQRCode, context:nil, options:[CIDetectorAccuracy: CIDetectorAccuracyHigh])
        var decode = ""
        var ciImage:CIImage = CIImage(image: ImgChoosed.image)
        var message:String = "";

        let features = detector.featuresInImage(ciImage)
        for feature in features as [CIQRCodeFeature] {
            message += feature.messageString
        }

        if(message == "") {
            println("nothing")

        } else {
            println("\(message)")
        }



    }

Have you a solution? Thank in advance guy's


回答1:


This happened to us as well. iPhone 4s doesn't return a CIDetector of type QRCode. The other types (rectangle, face) work though…

The same code works as expected on the iPhone 6. Haven't tested on a 5 or 5s yet.

But two weeks ago it was still working on the 4s, I believe. It was still on iOS 8 back then, I guess.




回答2:


The code you provided can't have a nil detector because it's not an optional and the compiler would complain about several places in your code if it was.

If features is empty then you know it didn't find a QR code in your image. Try providing a better image or turning down the CIDetectorAccuracy.

If features isn't empty then your cast is failing.

Edit: You can't pass a nil context in the constructor.




回答3:


  1. Make sure your ImgChoosed.image is not nil.
  2. Change another input image for testing.
  3. Try for feature in features as! [CIQRCodeFeature].



回答4:


I found that using on a device resolved this issue. The simulator seemed to always return nil for me.



来源:https://stackoverflow.com/questions/28973865/swift-no-matter-what-i-do-cidetector-is-always-nil

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!