You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何快速打开ViewController且不执行其流程?媒体文件夹加载优化方案

Hey there! Let's break down your two iOS development questions with practical, community-proven solutions:

1. 如何快速打开ViewController而不执行其相关流程?

The key here is to separate lightweight UI setup from heavy business logic in your ViewController's lifecycle methods. Here's how to do it:

  • First, only initialize the basic UI skeleton in viewDidLoad—think empty table/collection views, placeholder containers, or static UI elements that render instantly. Avoid any data fetching, file scanning, or media processing here.
  • Extract all the "related流程" (like API calls, local file parsing, or complex data setup) into a separate method, say loadContentAndSetup().
  • Push/present the ViewController immediately after initialization. Then trigger the extracted method after the UI has fully rendered—you can use viewDidAppear or a tiny delay to ensure the screen isn't blocked:
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // Add a small delay if needed to guarantee UI is ready
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            self.loadContentAndSetup()
        }
    }
    
  • If you need to completely skip certain logic for some scenarios, add an initialization flag to control it:
    class TargetVC: UIViewController {
        private let skipDefaultSetup: Bool
        
        init(skipDefaultSetup: Bool = false) {
            self.skipDefaultSetup = skipDefaultSetup
            super.init(nibName: nil, bundle: nil)
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        override func viewDidLoad() {
            super.viewDidLoad()
            setupBasicUI() // Always run this for the skeleton
            
            if !skipDefaultSetup {
                runDefaultBusinessLogic() // Only run if needed
            }
        }
    }
    
2. 解决大量媒体文件夹加载卡顿:先打开VC再展示加载过程

This builds on the first question's logic—prioritize user feedback first, then load content asynchronously. Here's a step-by-step implementation:

  • Show a loading state immediately: When the VC opens, display a lightweight loading UI (like an activity indicator, skeleton screen, or "Loading..." text) instead of the actual media list. This tells users the app is working, not frozen.
  • Load media content in the background: Move all file scanning/preview generation to a background thread to avoid blocking the main thread:
    private func startLoadingMedia(from folderPath: String) {
        DispatchQueue.global(qos: .userInitiated).async {
            // Heavy work here: scan folder, generate thumbnails for images/videos
            let mediaItems = self.scanMediaFolder(at: folderPath)
            
            // Update UI only on the main thread
            DispatchQueue.main.async {
                self.mediaList = mediaItems
                self.collectionView.reloadData()
                self.loadingView.isHidden = true
                self.collectionView.isHidden = false
            }
        }
    }
    
  • Optimize media previews: Don't load full-resolution images or raw video files upfront. Use system APIs to generate low-res thumbnails quickly:
    • For photos: Use PHImageManager with a target size to fetch thumbnails instead of originals.
    • For videos: Extract the first frame as a preview image asynchronously.
  • Add incremental loading: Instead of waiting for all files to load, load the first 20-30 items first, then continue loading the rest in the background. Refresh the UI incrementally so users can start interacting sooner.
  • Show loading progress: Optionally, add a progress label (e.g., "Loaded 15/80 files") to keep users informed during long scans.

内容的提问来源于stack exchange,提问作者user11770335

火山引擎 最新活动