🍣

AppleのサンプルコードAVCamでSemanticSegmentationMatteを選択すると出るエラーを修正

2020/11/06に公開

iPhone 12 Proがやって来たので、数年ぶりにカメラアプリを作ろうと思いAppleのサンプルAVCamをダウンロードしてXcode 12.1でビルドしてみた。

アプリが落ちる現象と原因

アプリを起動後、SSMアイコンをタップしてSemanticSegmentationMatteを選択しようとするとエラーで落ちてしまう。

Fatal error: Unknown matte type specified.: file AVCam/ItemSelectionViewController.swift, line 78

ログから問題の部分はItemSelectionViewController.swiftの78行目とわかる。

ItemSelectionViewController.swift
        switch ssmType {
        case .hair:
            cell.textLabel?.text = "Hair"
        case .teeth:
            cell.textLabel?.text = "Teeth"
        case .skin:
            cell.textLabel?.text = "Skin"
        default:
            fatalError("Unknown matte type specified.")
        }

ssmTypeに入っている値が想定されているものでないためにfatalErrorが呼ばれている。
まずはssmTypeに何が渡されているのが確認。

ssmType	AVSemanticSegmentationMatte.MatteType	
_rawValue	NSMutableString	"AVSemanticSegmentationMatteTypeGlasses"	0x0000000208d44408

ssmTypeAVSemanticSegmentationMatteTypeGlasses
そしてこれは現在はAVSemanticSegmentationMatte.MatteType.glassesに置き換わっている。

と言うわけで.glassesの場合も加えれば問題は解決する。

ItemSelectionViewController.swift
        switch ssmType {
        case .hair:
            cell.textLabel?.text = "Hair"
        case .teeth:
            cell.textLabel?.text = "Teeth"
        case .skin:
            cell.textLabel?.text = "Skin"
        case .glasses:
            cell.textLabel?.text = "Glasses"
        default:
            fatalError("Unknown matte type specified.")
        }

筈なのだが、ここでXcodeがエラーを表示してビルドができない。

'glasses' is only available in iOS 14.1 or newer

確認するとDeployment Targetが13.0になっていたのでこれを14.1にするとエラーは消えた。
これでエラーなくビルド完了。SSMアイコンをタップしても落ちなくなった。

しかし、実はこの状態で写真を撮っても下記のログが表示される。

This semantic segmentation type is not supported!

これはPhotoCaptureProcessorからのもの。
ItemSelectionViewController.swiftと同じようにPhotoCaptureDelegate.swiftにも同じssmTypeswitch文がある。これを修正すればログは表示されなくなる。

PhotoCaptureDelegate.swift
        switch ssmType {
        case .hair:
            imageOption = .auxiliarySemanticSegmentationHairMatte
        case .skin:
            imageOption = .auxiliarySemanticSegmentationSkinMatte
        case .teeth:
            imageOption = .auxiliarySemanticSegmentationTeethMatte
        case .glasses:
            imageOption = .auxiliarySemanticSegmentationGlassesMatte
        default:
            print("This semantic segmentation type is not supported!")
            return
        }

最後に

今回はとりあえずDeployment Targetを14.1にしてエラーを消したが、リリースするアプリが古いバージョンもサポートする場合は場合わけをする必要がある。

PhotoCaptureDelegate.swift
        if #available(iOS 14.1, *) {
            switch ssmType {
            case .hair:
                imageOption = .auxiliarySemanticSegmentationHairMatte
            case .skin:
                imageOption = .auxiliarySemanticSegmentationSkinMatte
            case .teeth:
                imageOption = .auxiliarySemanticSegmentationTeethMatte
            case .glasses:
                imageOption = .auxiliarySemanticSegmentationGlassesMatte
            default:
                print("This semantic segmentation type is not supported!\nssmType: \(ssmType)")
                return
            }
        } else {
            // Fallback on earlier versions
            switch ssmType {
            case .hair:
                imageOption = .auxiliarySemanticSegmentationHairMatte
            case .skin:
                imageOption = .auxiliarySemanticSegmentationSkinMatte
            case .teeth:
                imageOption = .auxiliarySemanticSegmentationTeethMatte
            default:
                print("This semantic segmentation type is not supported!\nssmType: \(ssmType)")
                return
            }
        }

同じコードが続いて気持ち悪いが仕方がない…

Discussion