gpt4 book ai didi

ios - UIImage(contentsOfFile) 的内存问题

转载 作者:行者123 更新时间:2023-11-30 13:30:34 25 4
gpt4 key购买 nike

我有一个 Collection View ,可以从文档目录加载一些图像:

func reloadNotesFromStore() {
{

let fetchRequest = NSFetchRequest(entityName: "TaskDraw")

let formsPredicate = NSPredicate(format: "task_id = %@", self.task_id)
fetchRequest.predicate = formsPredicate

let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: false)
let sortDescriptors = [sortDescriptor]
fetchRequest.sortDescriptors = sortDescriptors

self.photos.removeAll(keepCapacity: false)

let taskDrawings = (try! context.executeFetchRequest(fetchRequest)) as! [TaskDraw]

let path = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL

for taskDraw in taskDrawings {

let imageFilename = "\(taskDraw.remoteID!)_combined.png"
let imageFullPath = path.URLByAppendingPathComponent("\(self.task_id)/\(imageFilename)")

// create combined image to display
let photo = Photo(image: UIImage(contentsOfFile: imageFullPath.path!)!)

photo.selected = false
photo.taskDraw = taskDraw

self.photos.append(photo)
}

} ~> {
self.collectionView?.reloadData()
}

}

每次当我显示我的 CollectionViewController 时,我的内存使用量都会增加大约 30 MB。

我使用 UIImage(contentsOfFile) 加载的图像约为 150 kb。

好吧,没有尝试 Leos 解决方案,但仍然没有成功。当我重新加载我的 Controller (模态)时,我的内存总是增加约 30-40 MB。

func reloadNotesFromStore() {
{

let fetchRequest = NSFetchRequest(entityName: "TaskDraw")

let formsPredicate = NSPredicate(format: "task_id = %@", self.task_id)
fetchRequest.predicate = formsPredicate

let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: false)
let sortDescriptors = [sortDescriptor]
fetchRequest.sortDescriptors = sortDescriptors

self.items.removeAll(keepCapacity: false)

let taskDrawings = (try! context.executeFetchRequest(fetchRequest)) as! [TaskDraw]

let path = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL

for taskDraw in taskDrawings {

let imageFilename = "\(taskDraw.remoteID!)_combined.png"
let imageFullPath = path.URLByAppendingPathComponent("\(self.task_id)/\(imageFilename)")

// create combined image to display
let collectionViewItem = CollectionViewItem()
collectionViewItem.filePath = imageFullPath
collectionViewItem.selected = false
collectionViewItem.taskDraw = taskDraw

self.items.append(collectionViewItem)
}

} ~> {
self.collectionView?.reloadData()
}

}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PhotoCell", forIndexPath: indexPath) as! PhotoCell

let item = items[indexPath.item]

cell.imageView.image = UIImage(contentsOfFile: item.filePath!.path!)!
.....

我如何强制 Swift 释放我的内存?更新2:

当我使用 UIImage(named: ..) 而不是 UIImage(contentsOfFile: ....) 时,第一次初始化时内存会增加约 40 MB,但当我关闭 View 并再次加载时,内存会保持不变(我猜发生这种情况是因为命名:..缓存图像)

但这也不会在错误关闭 VC 时释放内存。

最佳答案

好的,我现在使用以下解决方案解决了问题:

我仍然在内存中创建一个 UIImage,如下所示:

for taskDraw in taskDrawings {

let imageFilename = "\(taskDraw.remoteID!)_combined.png"
let imageFullPath = path.URLByAppendingPathComponent("\(self.task_id)/\(imageFilename)")

let photo = Photo(image: UIImage(contentsOfFile: imageFullPath.path!)!.resizeToWidth(300))
photo.taskDraw = taskDraw
photo.addNote = false
self.photos.append(photo)

因此仅在 CollectionView 中显示调整大小的图像。 (否则我需要在磁盘上存储缩略图)

还有:

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.photos.removeAll(keepCapacity: false)
}

在 Controller 中。

现在,内存消耗相当低,并且在关闭我的 ViewController 后,已使用的内存现在被释放。

关于ios - UIImage(contentsOfFile) 的内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36611542/

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