AppleのサンプルコードAVCamでSemanticSegmentationMatteを選択すると出るエラーを修正
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行目とわかる。
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
ssmType
はAVSemanticSegmentationMatteTypeGlasses
。
そしてこれは現在はAVSemanticSegmentationMatte.MatteType.glasses
に置き換わっている。
と言うわけで.glasses
の場合も加えれば問題は解決する。
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にも同じssmType
のswitch
文がある。これを修正すればログは表示されなくなる。
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にしてエラーを消したが、リリースするアプリが古いバージョンもサポートする場合は場合わけをする必要がある。
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