我有一个以编程方式进行的collectionView,其中我有四种单元格类型,每种类型都包含一个图像 .
我通过indexPath.item替换这些单元格 .
当我通过单元格以节省内存时,我希望它们从内存中释放 .
使用当前代码,单元格图像在内存中分配,仅在我退出此ViewController时才会释放,但我希望它们在CollectionView中取消分配 .
我看到它可能是由于UIImage(命名为:)所以我切换到UIImage(contentOfFile),但它继续以这种方式 .
我能做的最接近的是声明一个全局UIImageView并移动到单元格,在每个单元格内部我将一个弱变量保存到这个UIImageView,但图像不会出现,只有文本保留 .
当用户滚动收集视图时,有谁知道如何释放每个单元格图像的内存?
代码:
import UIKit
class Gallery_ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
lazy var galleryCollection: UICollectionView = {
let collectionLayout = UICollectionViewFlowLayout()
collectionLayout.scrollDirection = .horizontal
return UICollectionView(frame: .zero, collectionViewLayout: collectionLayout)
}()
var imageView = UIImageView()
let paths: [String] = [Bundle.main.path(forResource: “img1", ofType: "png", inDirectory: "Images")!,
Bundle.main.path(forResource: “img2", ofType: "png", inDirectory: "Images")!,
Bundle.main.path(forResource: “img3", ofType: "png", inDirectory: "Images")!,
Bundle.main.path(forResource: “img4”, ofType: "jpg", inDirectory: "Images")!]
// Called before showing the view
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
/// Setup
func setup() {
setup_views()
}
/// Setup views
func setup_views() {
galleryCollection.dataSource = self
galleryCollection.delegate = self
galleryCollection.isPagingEnabled = true
galleryCollection.showsHorizontalScrollIndicator = false
galleryCollection.register(Gallery_1_Cell.self, forCellWithReuseIdentifier: “GalleryCell_1")
galleryCollection.register(Gallery_2_Cell.self, forCellWithReuseIdentifier: “GalleryCell_2”)
galleryCollection.register(Gallery_3_Cell.self, forCellWithReuseIdentifier: “GalleryCell_3")
galleryCollection.register(Gallery_Final_Cell.self, forCellWithReuseIdentifier: “GalleryCell_Final")
view.addSubview(galleryCollection)
// apperance
galleryCollection.backgroundColor = .white
// constraints
galleryCollection.translatesAutoresizingMaskIntoConstraints = false
view.addConstraint(NSLayoutConstraint(
item: galleryCollection, attribute: .centerX, relatedBy: .equal,
toItem: view, attribute: .centerX,
multiplier: 1,
constant: 0))
view.addConstraint(NSLayoutConstraint(
item: galleryCollection, attribute: .centerY, relatedBy: .equal,
toItem: view, attribute: .centerY,
multiplier: 1,
constant: 0))
view.addConstraint(NSLayoutConstraint(
item: galleryCollection, attribute: .width, relatedBy: .equal,
toItem: view, attribute: .width,
multiplier: 1,
constant: 0))
view.addConstraint(NSLayoutConstraint(
item: galleryCollection, attribute: .height, relatedBy: .equal,
toItem: view, attribute: .height,
multiplier: 1,
constant: 0))
}
// Returning Number of Cells
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
// Returning Cells
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print("Index: \(indexPath.item)")
print("Index Row: \(indexPath.row)")
//let imageV = UIImageView()
//imageView = imageV
if indexPath.item == 0 {
print("0000")
// creating cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GalleryCell_1", for: indexPath) as? Gallery_1_Cell
cell?.stringImagem = paths[indexPath.item]
cell?.imagem = UIImage(contentsOfFile: paths[indexPath.item])
cell?.backgroundImage = imageView
// passing collection view
//cell.backgroundImage = imageView
cell?.parentDelegate = self
// set custom apperance
cell?.setCustomApperance()
return cell!
}
if indexPath.item == 1 {
// creating cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GalleryCell_2”, for: indexPath) as? Gallery_2_Cell
print("1111")
cell?.stringImagem = paths[indexPath.item]
cell?.imagem = UIImage(contentsOfFile: paths[indexPath.item])
cell?.backgroundImage = imageView
// passing collection view
//cell.backgroundImage = imageView
cell?.parentDelegate = self
// set custom apperance
cell?.setCustomApperance()
return cell!
}
if indexPath.item == 2 {
// creating cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GalleryCell_3”, for: indexPath) as? Gallery_3_Cell
print("2222")
cell?.stringImagem = paths[indexPath.item]
cell?.imagem = UIImage(contentsOfFile: paths[indexPath.item])
cell?.backgroundImage = imageView
// passing collection view
cell?.parentDelegate = self
// set custom apperance
cell?.setCustomApperance()
return cell!
}
if indexPath.item == 3 {
// creating cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GalleryCell_Final", for: indexPath) as? Gallery_Final_Cell
print("3333")
cell?.stringImagem = paths[indexPath.item]
cell?.imagem = UIImage(contentsOfFile: paths[indexPath.item])
cell?.backgroundImage = imageView
// pass delegate
//cell.backgroundImage = imageView
cell?.parentDelegate = self
// set custom apperance
cell?.setCustomApperance()
return cell!
}
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
}
// Returning Cells Size
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
// Setting the Space between lines
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
// Setting the Inter Space between lines
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
下面是我使用的单元模型,在四种情况下几乎相同,所以我只会放一个 .
import UIKit
class Gallery_1_Cell: UICollectionViewCell
{
// views
var backgroundImage: UIImageView?
weak var imagem : UIImage?
var stringImagem:String?
// Initializer
override init(frame: CGRect) {
super.init(frame: frame)
}
// Serializer
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareForReuse() {
print("Cell1")
// self.subviews.forEach({ $0.removeFromSuperview() })
for v in self.subviews{
print("Cell1 Removeu")
print("V: \(v)")
v.removeFromSuperview()
}
}
/// Setup
func setup() {
setup_views()
setup_proportionalFonts()
}
/// Setup views
func setup_views() {
// Main View
self.clipsToBounds = true
// Background Image
let im = UIImage(contentsOfFile: stringImagem!)
backgroundImage?.image = im
self.addSubview(backgroundImage!)
// apperance
backgroundImage?.contentMode = .scaleAspectFill
backgroundImage?.backgroundColor = .red
// constraints
backgroundImage?.translatesAutoresizingMaskIntoConstraints = false
self.addConstraint(NSLayoutConstraint(
item: backgroundImage!, attribute: .centerX, relatedBy: .equal,
toItem: self, attribute: .centerX,
multiplier: 1,
constant: 0))
self.addConstraint(NSLayoutConstraint(
item: backgroundImage!, attribute: .centerY, relatedBy: .equal,
toItem: self, attribute: .centerY,
multiplier: 1,
constant: 0))
self.addConstraint(NSLayoutConstraint(
item: backgroundImage!, attribute: .width, relatedBy: .equal,
toItem: self, attribute: .width,
multiplier: 1,
constant: 0))
self.addConstraint(NSLayoutConstraint(
item: backgroundImage!, attribute: .height, relatedBy: .equal,
toItem: self, attribute: .height,
multiplier: 1,
constant: 0))
/// Set Custom Apperance
func setCustomApperance() {
setup()
}
}