使用 NSInvalidArgumentException“[_UIReplicantView _isSymbolImage]:无法识别的选择器发送到实例”时,快照 `UIView` 会导致应用程序崩溃
Snapshotting a `UIView` crashes the app with `NSInvalidArgumentException` "[_UIReplicantView _isSymbolImage]: unrecognized selector sent to instance"
大家好,我刚刚从 App Store 将 XCode 更新到版本 11.4,当我尝试在 iOS 13.4 上拍摄 UIView
快照时,如下所示:
extension UIView {
func snapshot(at scale: CGFloat) -> UIImage? {
let renderer = UIGraphicsImageRenderer(size: bounds.size)
let image = renderer.image { [weak self] context in
self?.drawHierarchy(in: self?.bounds ?? .zero, afterScreenUpdates: true)
}
return image
}
}
或类似的:
extension UIView {
func snapshotOld(at scale: CGFloat) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, scale)
guard let currentContext = UIGraphicsGetCurrentContext() else { return nil }
layer.render(in: currentContext)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
并将生成的图像设置为 UIImageView
,如下所示:
class ViewController: UIViewController {
@IBOutlet private weak var imageView: UIImageView!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let image = view.snapshot
imageView.image = image
}
}
extension UIView {
@objc var snapshot: UIImage? {
snapshot(at: 3.0)
// snapshotOld(at: 3.0)
}
}
我得到:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIReplicantView _isSymbolImage]: unrecognized selector sent to instance 0x115907c00'
*** First throw call stack:
(0x1ad52c164 0x1ad240c1c 0x1ad42a7e0 0x1b16a5b6c 0x1ad53085c 0x1ad532b60 0x1b1af6cdc 0x1b1af714c 0x1b1af0b30 0x1025061a8 0x102506270 0x1b1010880 0x1b10112cc 0x1b0f25658 0x1b167fc10 0x1b166f13c 0x1b16a088c 0x1ad4a6c54 0x1ad4a18e4 0x1ad4a1d84 0x1ad4a1660 0x1b78b2604 0x1b167615c 0x102507f78 0x1ad31d1ec)
libc++abi.dylib: terminating with uncaught exception of type NSException
我在模拟器和真实设备上都得到了这个。
你们知道为什么会这样吗?我怀疑这是一个 XCode 11.4 错误,因为这段代码在旧版本
上运行良好
Here is a sample project if you would like to try it out on your machine
您没有调用您认为的方法。您有两个选择:
将所有地方的 snapshot
更改为 snapshott
(或其他明确的替代方案)。
否则,删除 @objc
指定。
做其中任何一件,一切都会好起来的。
大家好,我刚刚从 App Store 将 XCode 更新到版本 11.4,当我尝试在 iOS 13.4 上拍摄 UIView
快照时,如下所示:
extension UIView {
func snapshot(at scale: CGFloat) -> UIImage? {
let renderer = UIGraphicsImageRenderer(size: bounds.size)
let image = renderer.image { [weak self] context in
self?.drawHierarchy(in: self?.bounds ?? .zero, afterScreenUpdates: true)
}
return image
}
}
或类似的:
extension UIView {
func snapshotOld(at scale: CGFloat) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, scale)
guard let currentContext = UIGraphicsGetCurrentContext() else { return nil }
layer.render(in: currentContext)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
并将生成的图像设置为 UIImageView
,如下所示:
class ViewController: UIViewController {
@IBOutlet private weak var imageView: UIImageView!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let image = view.snapshot
imageView.image = image
}
}
extension UIView {
@objc var snapshot: UIImage? {
snapshot(at: 3.0)
// snapshotOld(at: 3.0)
}
}
我得到:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIReplicantView _isSymbolImage]: unrecognized selector sent to instance 0x115907c00'
*** First throw call stack:
(0x1ad52c164 0x1ad240c1c 0x1ad42a7e0 0x1b16a5b6c 0x1ad53085c 0x1ad532b60 0x1b1af6cdc 0x1b1af714c 0x1b1af0b30 0x1025061a8 0x102506270 0x1b1010880 0x1b10112cc 0x1b0f25658 0x1b167fc10 0x1b166f13c 0x1b16a088c 0x1ad4a6c54 0x1ad4a18e4 0x1ad4a1d84 0x1ad4a1660 0x1b78b2604 0x1b167615c 0x102507f78 0x1ad31d1ec)
libc++abi.dylib: terminating with uncaught exception of type NSException
我在模拟器和真实设备上都得到了这个。
你们知道为什么会这样吗?我怀疑这是一个 XCode 11.4 错误,因为这段代码在旧版本
上运行良好Here is a sample project if you would like to try it out on your machine
您没有调用您认为的方法。您有两个选择:
将所有地方的
snapshot
更改为snapshott
(或其他明确的替代方案)。否则,删除
@objc
指定。
做其中任何一件,一切都会好起来的。