我有一个集合视图在那里我加载某些图像从我的文档目录:
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()
}
}
每当约30 MB生病显示我的CollectionViewController,我的内存使用量增加。
我用的UIImage(contentsOfFile)加载的图像是约150 KB。
好吧试过没有狮子座的解决方案,但仍然没有成功。 生病时我重新加载控制器(模态)我的记忆总是增加大约30-40兆字节。
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!)!
.....
我怎样才能迫使斯威夫特来释放我的记忆? UPDATE2:
生病时使用的UIImage(名为:..),而不是UIImage的(contentsOfFile:....),那么在第一次初始化了约40 MB的内存增加,但保持生病时关闭该视图,重新装入,(我想这是因为命名:..缓存图像)
但是,这也不会释放生病时驳回VC的内存。