ios - 根据矩形请求设置属性。 iOS, swift

标签 ios swift

我正在尝试根据矩形请求设置属性。 我正在尝试设置最小尺寸、宽高比和其他设置,但出现以下错误。

Fatal error: Attempted to read an unowned reference but the object was already deallocated2018-08-13 16:08:09.081049+0100 app[4000:1277980] Fatal error: Attempted to read an unowned reference but the object was already deallocated

func performVisionRequest(image: CGImage, orientation: CGImagePropertyOrientation) {

    DispatchQueue.global(qos: .userInitiated).async {
        do {
            let imageRequestHandler = VNImageRequestHandler(cgImage: image, orientation: orientation, options: [:])
            try imageRequestHandler.perform(
                [VNDetectRectanglesRequest(completionHandler:{ req, err in
                    self.rectanglesRequest(request:req, error:err)

                })]
            )
        } catch let error as NSError {
            self.sliceCompletion([UIImage]())
            print("Failed to perform vision request: \(error)")

        }
    }
}

func rectanglesRequest(request: VNRequest, error: Error?) {
    if let err = error as NSError? {
        noRect = true
        var slices = [imageNo]
        self.sliceCompletion(slices as! [UIImage])
        slices = []
        print("Failed during detection: \(err.localizedDescription)")
        return
    }
    unowned let rectanglesRequestSet = VNDetectRectanglesRequest()

    rectanglesRequestSet.minimumSize = 0.07
    rectanglesRequestSet.minimumAspectRatio = 0.2
    rectanglesRequestSet.maximumAspectRatio = 0.3
    rectanglesRequestSet.quadratureTolerance = 22.0

最佳答案

这里将 rectanglesRequestSet 标记为 unowned 是不好的,因为它会导致它在您可以使用它之前以及当您尝试向已释放的消息发送消息时被释放 unowned 对象你会得到一个崩溃,这解释了你得到的消息:

Attempted to read an unowned reference but the object was already deallocated

unowned 用于打破当类的属性保留对类本身的引用时发生的保留循环,通常是通过闭包。例如:

class ViewControllerA: UIViewController {
    let viewControllerB = UIViewControllerB()

    override func viewDidLoad() {
        super.viewDidLoad()
        viewControllerB.onSelect = {
            self.onSelection() 
        }
    }

    func onSelection() {
        // do something
    }
}

class ViewControllerB: UIViewController {
    var onSelect: (() -> Void)?
}

例如,上面的代码创建了一个保留周期 vcA -> vcB -> onSelect -> vcA,其中 vcAViewControllerA 的实例,vcBViewControllerB 的实例。 vcAvcB 永远不会被释放,因为 vcA 持有对 vcB 的引用,因为它是一个属性。并且,vcB 通过 onSelect 闭包中的变量捕获保存对 vcA 的引用。发生这种情况是因为为了让闭包在将来执行代码,它们必须持有对闭包中使用的所有对象的引用,在示例中 vcA 是唯一使用的对象,因此闭包持有引用到它并且 vcB 持有对闭包的引用。为了防止这个保留周期:

viewControllerB.onSelect = { [unowned self]
    self.onSelection() 
}

viewControllerB.onSelect = { [weak self]
    self?.onSelection() 
}

将导致闭包无法捕获 vcA,这意味着将没有保留周期。使用 weak 而不是 unowned 更安全。闭包不能保证未捕获的对象会在执行时出现 weak 将允许此类对象为 nilunowned is, in一种方式,规定它不会为 nil 并指示程序在为 nil 时崩溃。

关于ios - 根据矩形请求设置属性。 iOS, swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51825489/

相关文章:

ios - swift 如何知道要导入哪些文件?

ios - 非 -'@objc' 方法不满足 '@objc' 协议(protocol)的要求

ios - swift:从 JSON 解析的 Alphabetiz 数据

ios - 如何将图像数组转换为字符串

ios - OpenGL ES : Do vertex structs need x, y 和 z?

ios - Swift - 将 NSURLResponse 向下转换为 NSHTTPURLResponse 以获得响应代码

ios - 使 UIView 填充 UIScrollview

ios - Xcode 9. 界面生成器。已安装的复选标记丢失

ios - didReceiveRemoteNotification 函数不使用 FCM 通知服务器调用

ios - 如何使用这个复杂的参数方程在 iOS 中绘制曲线