gpt4 book ai didi

ios - 内存泄漏,在 do-catch block 中。 iOS, swift

转载 作者:行者123 更新时间:2023-11-28 14:25:57 26 4
gpt4 key购买 nike

我正在查看使用视觉检测文本的应用程序中的内存泄漏。

我遇到了内存泄漏,当使用树指向这一行时:

try imageRequestHandler.perform([self.textDetectionRequest])

我不确定为什么,希望有人能帮助解决这个问题。

完整代码如下。

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

DispatchQueue.global(qos: .userInitiated).async {
do {
var imageRequestHandler = VNImageRequestHandler(cgImage: image, orientation: orientation, options: [:])
try imageRequestHandler.perform([self.textDetectionRequest])
} catch let error as NSError {
print("Failed to perform vision request: \(error)")

}
}
}

这是全类:

import UIKit
import Vision

var noText: Bool!
var imageNo: UIImage!

internal class Slicer {

private var image = UIImage()
private var sliceCompletion: ((_ slices: [UIImage]) -> Void) = { _ in }

private lazy var textDetectionRequest: VNDetectTextRectanglesRequest = {

return VNDetectTextRectanglesRequest(completionHandler: self.handleDetectedText)
}()

internal func slice(image: UIImage, completion: @escaping ((_: [UIImage]) -> Void)) {
self.image = image
self.sliceCompletion = completion
self.performVisionRequest(image: image.cgImage!, orientation: .up)
}

// MARK: - Vision

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

DispatchQueue.global(qos: .userInitiated).async {
do {
let imageRequestHandler = VNImageRequestHandler(cgImage: image, orientation: orientation, options: [:])
try imageRequestHandler.perform([self.textDetectionRequest])
} catch let error as NSError {
self.sliceCompletion([UIImage]())
print("Failed to perform vision request: \(error)")

}
}
}

private func handleDetectedText(request: VNRequest?, error: Error?) {
if let err = error as NSError? {
print("Failed during detection: \(err.localizedDescription)")
return
}
guard let results = request?.results as? [VNTextObservation], !results.isEmpty else {

noText = true
print("Tony no text found")
var slices = [imageNo]
self.sliceCompletion(slices as! [UIImage])
slices = []
return }

noText = false
self.sliceImage(text: results, onImageWithBounds: CGRect(x: 0, y: 0, width: self.image.cgImage!.width, height: self.image.cgImage!.height))
}

private func sliceImage(text: [VNTextObservation], onImageWithBounds bounds: CGRect) {
CATransaction.begin()

var slices = [UIImage]()

for wordObservation in text {
let wordBox = boundingBox(forRegionOfInterest: wordObservation.boundingBox, withinImageBounds: bounds)

if !wordBox.isNull {
guard let slice = self.image.cgImage?.cropping(to: wordBox) else { continue }
slices.append(UIImage(cgImage: slice))
}
}

self.sliceCompletion(slices)

CATransaction.commit()
}

private func boundingBox(forRegionOfInterest: CGRect, withinImageBounds bounds: CGRect) -> CGRect {

let imageWidth = bounds.width
let imageHeight = bounds.height

// Begin with input rect.
var rect = forRegionOfInterest

// Reposition origin.
rect.origin.x *= imageWidth
rect.origin.y = ((1 - rect.origin.y) * imageHeight) - (forRegionOfInterest.height * imageHeight)

// Rescale normalized coordinates. Tony adde + 30 to increase the size of rect
rect.size.width *= imageWidth + 30
rect.size.height *= imageHeight + 30

return rect
}
}

enter image description here

最佳答案

其他人告诉您的都是正确的。您有两个引用 self(切片器)实例的闭包,您需要打破它们中的保留循环。我认为这条线是一个巨大的错误:

private lazy var textDetectionRequest: VNDetectTextRectanglesRequest = {
return VNDetectTextRectanglesRequest(completionHandler: self.handleDetectedText)
}()

除了保留循环之外,你什么也得不到。删除那些行!相反,只需在需要时创建匿名函数即可。替换为:

try imageRequestHandler.perform([self.textDetectionRequest])

用这个:

try imageRequestHandler.perform(
[VNDetectTextRectanglesRequest(completionHandler:{ req, err in
self.handleDetectedText(request:req, error:err)
})]
)

如果仍然存在泄漏(我对此表示怀疑),则将其更改为

try imageRequestHandler.perform(
[VNDetectTextRectanglesRequest(completionHandler:{ [weak self] req, err in
self?.handleDetectedText(request:req, error:err)
})]
)

关于ios - 内存泄漏,在 do-catch block 中。 iOS, swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51560767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com