由于自定义脚本而导致的常见iOS内存泄漏问题。如何解决?

问题描述 投票:0回答:1

我在StackOverflow上看到了以下代码的变体:

import UIKit

class segueFromLeft: UIStoryboardSegue {
    override func perform() {
        // Assign the source and destination views to local variables.
        let src = self.source.view as UIView!
        let dst = self.destination.view as UIView!

        // Get the screen width and height.
        let screenWidth = UIScreen.main.bounds.size.width
        let screenHeight = UIScreen.main.bounds.size.height

        // Specify the initial position of the destination view.
        dst?.frame = CGRect(x: screenWidth, y: 0, width: screenWidth,
height: screenHeight)

        // Access the app's key window and insert the destination view
above the current (source) one.
        let window = UIApplication.shared.keyWindow
        window?.insertSubview(dst!, aboveSubview: src!)

        // Animate the transition.
        UIView.animate(withDuration: 0.5, animations: { () -> Void in
            src?.frame = (src?.frame.offsetBy(dx: -screenWidth, dy: 0))!
            dst?.frame = (dst?.frame.offsetBy(dx: -screenWidth, dy: 0))!

        }) { (Finished) -> Void in

            self.source.present(self.destination, animated: false, completion: nil) {

            }

        }

    }
}

起初,代码是从一个视图过渡到另一个视图的一种很好的方式。但是随着使用的不断进行,该网站上列出的大多数问题都与内存有关。每次使用segue时,都会初始化目标视图,而源视图仍保留在内存中。随着持续使用,内存使用量继续增长。

对源视图的简单解雇对我来说不起作用,屏幕只是变黑了。

我的问题是,我们如何解决这个问题?

ios swift memory-management storyboard segue
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.