Ad

Our DNA is written in Swift
Jump

Core Image Barcode Erratum

Barcodes with iOSSince iOS 7, Core Image contains several generators for 2D barcodes. While I was writing my book Barcodes and iOS, only the CIQRCodeGenerator was documented. Apple’s standard policy is that classes which are not documented are to be considered off limites. Seeking clarification, I emailed an Apple evangelist and he confirmed that this is still the case.

So I mentioned the three other – undocumented – Core Image filters in the chapter about barcode generation, but cautioned the reader about their usage in app store apps: CIAztecCodeGenerator, CIPDF417BarcodeGenerator and CICode128BarcodeGenerator. At WWDC 2015, I learned that my assumption as well as the evangelist’s confirmation were incorrect.

I spoke with the Core Image engineering manager and he told me that there is an exception to the no-documentation rule for Core Image filters. You are alloc/init Core Image filter objects directly, but rather are using the filterNamed: method. The documentation team might lack behind in documenting certain filters, but that does not mean you cannot use them. He said that you can use the code below to find out which filters are available:

NSArray *b = [CIFilter filterNamesInCategory:kCICategoryGenerator];
NSLog(@"%@", b);

This means that these barcode generators are available, even when they are not (yet) documented:

  • CIQRCodeGenerator (iOS 7)
  • CIAztecCodeGenerator (iOS 8)
  • CICode128BarcodeGenerator (iOS 8)
  • CIPDF417BarcodeGenerator (iOS 9)

Technically, the private classes for these might have appeared in earlier iOS versions because iOS was using them internally for Passbook passes. But if filterNamesInCategory: method does not return them then they are off limits.

Who needs documentation anyway?

Should you wish to use a generator for which there is no documentation, you can find out about its input parameters with the handy attributes method.

CIFilter *filter = [CIFilter filterWithName:@"CIAztecCodeGenerator"];
NSDictionary *attributes = [filter attributes];
NSLog(@"%@", attributes);

The resulting dictionary tells you all you need to know:

  • CIAttributeClass is the class of the parameter
  • CIAttributeDefault is the default value if the parameter is omitted
  • CIAttributeMax is the maximum value for the parameter
  • CIAttributeMin is the minimum value for the parameter

Numeric values additionally provide CIAttributeSliderMin and CIAttributeSliderMax. If you are showing UI to configure this filter with a slider, those are the recommended limits.

Conclusion

There are more generators in Core Image that might be documented for a given iOS version. But if you can see them returned by this handy method then you may use them. Finding out the appropriate input parameters might take a bit of experimentation, but that is an easy exercise.

This is a fine example for why labs at WWDC are so valuable: it is the only place where you can talk to the guy in charge. After 4 months of my book being out, this is the very first errata.


Categories: Q&A

Leave a Comment