Snapshotting a `UIView` crashes the app with `NSInvalidArgumentException` “[_UIReplicantView _isSymbolImage]: unrecognized selector sent to instance”

强颜欢笑 提交于 2020-06-01 05:09:08

问题


Hi guys i have just updated XCode to version 11.4 from the app store and when i try to snapshot a UIView on iOS 13.4 like this:

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
    }
}

or like that:

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
    }
}

and set the resulting image to UIImageView like that:

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)
    }
}

i get:

*** 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

I get this both in simulator and on a real device.

Do you guys have any idea why is this happening? I suspect it is a XCode 11.4 bug because this code worked nicely on the older versions 🤔

Here is a sample project if you would like to try it out on your machine


回答1:


You're not calling the method you think you are. You have two choices:

  • Change snapshot to snapshott everywhere (or some other nonambiguous alternative).

  • Or else, remove the @objc designation.

Do either of those things, and all will be well.



来源:https://stackoverflow.com/questions/60870129/snapshotting-a-uiview-crashes-the-app-with-nsinvalidargumentexception-uir

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!