在 swift ios 中滑动后,有没有办法在 uipageviewcontroller 中取消分配页面?
Is there a way to deallocate a page in uipageviewcontroller after being swiped in swift ios?
我是 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)
更多内存管理概念可以参考这个link:
我是 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)
更多内存管理概念可以参考这个link: