M1チップのMacで「UIImagePickerController」で写真を選んで選択ボタンを押すとクラッシュする
notwork_imagepicker
スポンサーリンク

Apple Silicon(M1チップ)のMacでImagePickerの決定時にクラッシュしたときの解決方法

Apple Silicon (Apple M1チップ)のMac mini(macOS Big Sur)でUIImagePickerController関連のエラーが発生しました。
Xcodeのバージョンは12.4

シミュレーターで実行中、ImagePickerで選択ボタンを押したら[NSURL URLByAppendingPathExtension:]: component, components, or pathExtension cannot be nil.

ヒーラー
arm64のエラーを解決させた後でクラッシュするようになったので、影響がありそう

問題

以下のとおりの、なんの変哲もないUIImagePickerControllerを使って写真を選択するだけのコードで、クラッシュが発生しました。

{        
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true
    present(picker, animated: true, completion: nil)
}

...

// MARK: - UIImagePickerControllerDelegate

extension RegistrationController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        ...
    }
}

写真を選択し、「選択」ボタンを押したら、didFinishPickingMediaWithInfoが呼ばれる前に、クラッシュしました。詳細は以下のとおりです。

[xpc.exceptions] <NSXPCConnection: 0x600001c14820> connection on anonymousListener or serviceListener from pid 35285: Warning: Exception caught during invocation of selector didSelectMediaWithInfoDictionary:, dropping incoming message and invalidating the connection.
Exception: *** -[NSURL URLByAppendingPathExtension:]: component, components, or pathExtension cannot be nil.
*** -[NSURL URLByAppendingPathExtension:]: component, components, or pathExtension cannot be nil.
(
	0   CoreFoundation                      0x00007fff20421af6 __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x00007fff20177e78 objc_exception_throw + 48
	2   Foundation                          0x00007fff20860900 -[NSURL(NSURLPathUtilities) URLByDeletingPathExtension] + 0
	3   PhotosUI                            0x00007fff3faf656a -[PUPhotoPickerExtensionHostContext _UIImagePickerControllerInfoDictionaryFromPhotoPickerInfoDictionary:] + 2177
	4   PhotosUI                            0x00007fff3faf5b5b -[PUPhotoPickerExtensionHostContext didSelectMediaWithInfoDictionary:] + 34
	5   Foundation                          0x00007fff209290e3 __NSXPCCONNECTION_IS_CALLING_OUT_TO_EXPORTED_OBJECT_S1__ + 10
	6   Foundation                          0x00007fff20927e1b -[NSXPCConnection _decodeAndInvokeMessageWithEvent:flags:] + 2268
	7   Foundation                          0x00007fff20929412 message_handler + 206
	8   libxpc.dylib                        0x00007fff20055cac _xpc_connection_call_event_handler + 56
	9   libxpc.dylib                        0x00007fff2005603e _xpc_connection_mach_event + 905
	10  libdispatch.dylib                   0x0000000110a5fa68 _dispatch_client_callout4 + 9
	11  libdispatch.dylib                   0x0000000110a79ba8 _dispatch_mach_msg_invoke + 550
	12  libdispatch.dylib                   0x0000000110a660ad _dispatch_lane_serial_drain + 307
	13  libdispatch.dylib                   0x0000000110a7aa76 _dispatch_mach_invoke + 563
	14  libdispatch.dylib                   0x0000000110a660ad _dispatch_lane_serial_drain + 307
	15  libdispatch.dylib                   0x0000000110a66f9d _dispatch_lane_invoke + 493
	16  libdispatch.dylib                   0x0000000110a72de2 _dispatch_workloop_worker_thread + 882
	17  libsystem_pthread.dylib             0x00007fff61167499 _pthread_wqthread + 314
	18  libsystem_pthread.dylib             0x00007fff61166467 start_wqthread + 15
)

原因

最初はM1 Macのシミュレータでも動作確認できていたので、おそらく、CococaPodsでFirebase追加して、Excluded Architectures arm64の設定を追加してからの動作がおかしくなったので、arm64の除外が影響しているような気がします。

arm64に関する詳しい内容は以下の記事にあります。

M1チップのMacで「linker command failed with exit code 1 ('' for architecture arm64)」エラーが出る場合の解決方法

解決方法

結論としては、未解決です。

githubのQAによると、arm64の除外が怪しいというコメントと、M1 Macのシミュレータのバグだというコメントが見られます。M1 Macでまだ対応が追いついていない部分はありそうですね。

代替方法はあります。

  • 実機を使う
  • iOS13以前のシミュレーターを使う

実機では問題ありませんでした。
iOS13以前のシミュレータでも正常に動作しました。
※iOS13.7で動作確認して、設定そのままにイメージピッカーで選択可能でした。

バグが修正されるまで、代替方法でしのげれば幸いです。

参考

https://github.com/xamarin/Essentials/issues/1653

https://github.com/expo/expo/issues/11291

スポンサーリンク

Twitterでフォローしよう

おすすめの記事