Downcasting filters created using `CIFilter.perspectiveCorrection()` from `CIFilter` to `CIPerspectiveCorrection` fails

Originator:klausa
Number:rdar://FB761103 Date Originated:3 Mar 2020
Status:Open Resolved:
Product:Core Image API Product Version:
Classification:Incorrect/Unexpected Behavior Reproducible:
 
Downcasting `CIFilters` (or at least the `CIPerspectiveCorrection `, but I suspect the problem is broader) created using convenience methods defined in `CoreImage.CIFilterBuiltins` fails.

My app supports systems older than iOS 13, and I was hoping to support both pre-iOS 13 and the iOS 13 convenience API like so:

```
private let perspectiveFilter: CIFilter = {
        if #available(iOS 13, *) {
            return CIFilter.perspectiveCorrection()
        } else {
            return CIFilter(name: "CIPerspectiveCorrection")!
        }
    }()
```

which then would get used at callsite like such:

```
  if #available(iOS 13, *) {
            guard let filter = perspectiveFilter as? CIPerspectiveCorrection else {
                return nil
            }
```

To my surprise, this guard/let statement fails and returns nil.

I also confirmed this in a simple playground like so:

```
import UIKit
import CoreImage
import CoreImage.CIFilterBuiltins

let filter: CIFilter = CIFilter.perspectiveCorrection()

if let perspectiveFilter = filter as? CIPerspectiveCorrection {
    print("downcast successfull")
} else {
    print("failed")
}
```

which prints out “failed”. 

Tested on iPhone 11 Pro Max / iOS 13.3.1, and iOS 13.3 Simulator bundled with Xcode 11.3.1  (11C505).

Comments

I am not too sure about the #available macro-looking thing resolves, but more relevantly, CIPerspectiveCorrection is an obj-c protocol, not a class.

By kumowoon1025 at March 9, 2020, 4:07 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!