gpt4 book ai didi

ios - 在 swift ios 中滑动后,有没有办法在 uipageviewcontroller 中释放页面?

转载 作者:行者123 更新时间:2023-11-29 01:46:06 24 4
gpt4 key购买 nike

我是 swift iOS 的新手,我只想问一下是否有一种方法可以在滑动后删除或取消分配页面,因为每次滑动到下一页时它都会占用太多内存。我正在处理 64 个页面,因此如果我不应用释放,应用程序可能会卡住或崩溃。

ViewController.swift

import UIKit

class ViewController: UIViewController, UIPageViewControllerDataSource {

// MARK: - Variables
private var pageViewController: UIPageViewController?

// Initialize it right away here
private var contentImages = [
"CQ print version_Page_1.jpg",
"CQ print version_Page_2.jpg",
"CQ print version_Page_3.jpg",
"CQ print version_Page_4.jpg",
"CQ print version_Page_5.jpg",
"CQ print version_Page_6.jpg",
"CQ print version_Page_7.jpg",
"CQ print version_Page_8.jpg",
"CQ print version_Page_9.jpg",
"CQ print version_Page_10.jpg",
"CQ print version_Page_11.jpg",
"CQ print version_Page_12.jpg",
"CQ print version_Page_13.jpg",
"CQ print version_Page_14.jpg",
"CQ print version_Page_15.jpg",
"CQ print version_Page_16.jpg",
"CQ print version_Page_17.jpg",
"CQ print version_Page_18.jpg",
"CQ print version_Page_19.jpg",
"CQ print version_Page_20.jpg",
"CQ print version_Page_21.jpg",
"CQ print version_Page_22.jpg",
"CQ print version_Page_23.jpg",
"CQ print version_Page_24.jpg",
"CQ print version_Page_25.jpg",
"CQ print version_Page_26.jpg",
"CQ print version_Page_27.jpg",
"CQ print version_Page_28.jpg",
"CQ print version_Page_29.jpg",
"CQ print version_Page_30.jpg",
"CQ print version_Page_31.jpg",
"CQ print version_Page_32.jpg",
"CQ print version_Page_33.jpg",
"CQ print version_Page_34.jpg",
"CQ print version_Page_35.jpg",
"CQ print version_Page_36.jpg",
"CQ print version_Page_37.jpg",
"CQ print version_Page_38.jpg",
"CQ print version_Page_39.jpg",
"CQ print version_Page_40.jpg",
"CQ print version_Page_41.jpg",
"CQ print version_Page_42.jpg",
"CQ print version_Page_43.jpg",
"CQ print version_Page_44.jpg",
"CQ print version_Page_45.jpg",
"CQ print version_Page_46.jpg",
"CQ print version_Page_47.jpg",
"CQ print version_Page_48.jpg",
"CQ print version_Page_49.jpg",
"CQ print version_Page_50.jpg",
"CQ print version_Page_51.jpg",
"CQ print version_Page_52.jpg",
"CQ print version_Page_53.jpg",
"CQ print version_Page_54.jpg",
"CQ print version_Page_55.jpg",
"CQ print version_Page_56.jpg",
"CQ print version_Page_57.jpg",
"CQ print version_Page_58.jpg",
"CQ print version_Page_59.jpg",
"CQ print version_Page_60.jpg",
"CQ print version_Page_61.jpg",
"CQ print version_Page_62.jpg",
"CQ print version_Page_63.jpg",
"CQ print version_Page_64.jpg"];

//var contentImages = [String]()

// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
createPageViewController()
setupPageControl()
}

private func createPageViewController() {

let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as! UIPageViewController
pageController.dataSource = self

if contentImages.count > 0 {
let firstController = getItemController(0)!
let startingViewControllers: NSArray = [firstController]
pageController.setViewControllers(startingViewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}

pageViewController = pageController
addChildViewController(pageViewController!)
self.view.addSubview(pageViewController!.view)
pageViewController!.didMoveToParentViewController(self)
}

private func setupPageControl() {
let appearance = UIPageControl.appearance()
appearance.pageIndicatorTintColor = UIColor.grayColor()
appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
appearance.backgroundColor = UIColor.darkGrayColor()
}

// MARK: - UIPageViewControllerDataSource

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

let itemController = viewController as! PageItemController

if itemController.itemIndex > 0 {
return getItemController(itemController.itemIndex-1)
}

return nil
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

let itemController = viewController as! PageItemController

if itemController.itemIndex+1 < contentImages.count {
return getItemController(itemController.itemIndex+1)
}

return nil
}

private func getItemController(itemIndex: Int) -> PageItemController? {

if itemIndex < contentImages.count {
let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemController") as! PageItemController
pageItemController.itemIndex = itemIndex
pageItemController.imageName = contentImages[itemIndex]
return pageItemController
}

return nil
}

// MARK: - Page Indicator

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return contentImages.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return contentImages.count
}

}

PageItemController.swift

import UIKit

class PageItemController: UIViewController {

// MARK: - Variables
var itemIndex: Int = 0
var imageName: String = "" {

didSet {

if let imageView = contentImageView {
imageView.image = UIImage(named: imageName)
}

}
}

@IBOutlet var contentImageView: UIImageView?

// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
contentImageView!.image = UIImage(named: imageName)
}
}

最佳答案

如果这个内存问题与图像有关,那么,

正如文档所述:

If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

所以不要使用

contentImageView!.image = UIImage(named: imageName)

你应该使用

contentImageView!.image = UIImage(contentsOfFile: imageName)

有关更多内存管理概念,您可以引用此链接:Swift Managing Memory

关于ios - 在 swift ios 中滑动后,有没有办法在 uipageviewcontroller 中释放页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31914956/

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