1

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

Saad
  • 8,857
  • 2
  • 41
  • 51
Tumeco
  • 115
  • 2
  • 9

4 Answers4

1

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.

InkGolem
  • 2,662
  • 1
  • 15
  • 22
  • I know for the nil detector can't be nil, but this is what it is when I declare it like this. XCode tell me : "fatal error: unexpectedly found nil while unwrapping an Optional value" at the line of the detectorField. I tried to put CIDetectorAccuracy to low, but this is the same. and when I switch the "ofType: CIDetectorTypeQRCode" for CIDectectorTypeFace, everything compile. – Tumeco Mar 10 '15 at 21:12
  • @Tumeconnais That error is because you can't pass a `nil` context. – InkGolem Mar 10 '15 at 21:26
  • So I Put context:CIContext(options: nil) and same. I don't understand :( – Tumeco Mar 10 '15 at 21:34
  • @Tumeconnais if a parameter in `UIKit` is force unwrapped (ends with !) then you can't pass nil. The same goes for options in this case. – InkGolem Mar 10 '15 at 21:35
  • context:CIContext(options:[kCIContextPriorityRequestLow: true]) nothing with that neither, if I follow this tutorial [link](http://www.raywenderlich.com/76285/beginning-core-image-swift) I can put nil in context. But the things I don't understand is : if I put CIDetector(ofType: **CIDetectorTypeFace**, context:nil, options:[CIDetectorAccuracy: CIDetectorAccuracyHigh]) everythings works fine, and no error occurs. – Tumeco Mar 10 '15 at 21:43
  • follow/open the method with cmd + Click and you will see: init!(ofType type: String!, context: CIContext!, options: [NSObject : AnyObject]!) -> CIDetector, so you can't/shouldn't pass nil – LoVo Mar 10 '15 at 21:46
  • in the tutorial he first creates var context : CIContext! and in the viewdidload he init, context = CIContext(options:nil) – LoVo Mar 10 '15 at 21:50
  • I made everythings, and don't work, everythings work when I put CIDetectorTypeFace or CIDetectorTypeRectangle but not with CIDetectorTypeQRCode. I Think a lot that just a Bug about API because Swift is always in Beta. – Tumeco Mar 10 '15 at 22:04
  • I haven't solved, so I removed this functionnality from my app, now the user need to take a photo of the QRCode. – Tumeco Apr 08 '15 at 08:13
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.

leberwurstsaft
  • 405
  • 1
  • 5
  • 11
  • Tested in a iPhone 5 and didn't work. But worked on a iPad Air 2 though. Both running iOS 9. – Isuru Mar 15 '16 at 19:00
0
  1. Make sure your ImgChoosed.image is not nil.
  2. Change another input image for testing.
  3. Try for feature in features as! [CIQRCodeFeature].
Isuru
  • 30,617
  • 60
  • 187
  • 303
Paradise
  • 558
  • 6
  • 17
-2

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

waybu
  • 1