UIImage(contentsOfFile) 的内存问题 [英] Memory Problems with UIImage(contentsOfFile)
问题描述
我有一个集合视图,我从我的文档目录中加载了一些图像:
I have a Collection View where i load some Images from my documents directory:
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.
Every time when ill display my CollectionViewController, my Memory usage increases by about 30 MB.
我使用 UIImage(contentsOfFile) 加载的图像大约 150 kb.
The images that i load with UIImage(contentsOfFile) are about 150 kb.
好的,没有尝试 Leos 解决方案,但仍然没有成功.当我重新加载我的控制器(模态)时,我的内存总是增加大约 30-40 MB.
Ok tried no Leos Solution, but still no success. When ill reload my Controller (modally) my memory always increases about 30-40 Mbytes.
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:
How can i force Swift to release my memory? Update2:
当我使用 UIImage(named: ..) 而不是 UIImage(contentsOfFile: ....) 然后在第一次初始化时内存增加了大约 40 MB,但是当我关闭视图并再次加载它时会保持不变(我猜发生这种情况是因为 named: .. caches the image)
When ill use UIImage(named: ..) instead of UIImage(contentsOfFile: ....) then on first init the memory increase by about 40 Mbyte, but stays when ill dismiss the View and load it again (i guess this happens because named: .. caches the image)
但是这也不会在关闭 VC 时释放内存.
But this also do not release the memory when ill dismiss the VC.
推荐答案
好的,我现在用以下解决方案解决了这个问题:
Ok i fixed the problem now with the following solution:
我仍然在内存中创建一个 UIImage,如下所示:
I still create an UIImage in Memory, like this:
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 中显示调整大小的图像.(否则我需要在磁盘上存储缩略图)
So only show the resized image in the CollectionView. (Otherwise i need to store a Thumbnail on Disk)
还有:
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.photos.removeAll(keepCapacity: false)
}
在控制器中.
现在,内存消耗相当低,并且在关闭我的 ViewController 后,使用的内存现在被释放.
Now, the Memory consumption is quite lower AND after dismissing my ViewController, the used memory is now released.
这篇关于UIImage(contentsOfFile) 的内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!