内存问题的UIImage(contentsOfFile)(Memory Problems with

2019-11-04 05:50发布

我有一个集合视图在那里我加载某些图像从我的文档目录:

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的内存。

Answer 1:

好吧,我现在固定的问题与以下解决方案:

我仍然在内存中创建一个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)
}

在控制器。

现在,内存消耗是相当低,解聘我的ViewController后,使用的内存,现予以公布。



Answer 2:

我有一个图像文件的目录,我使用的UIImage(contentsOfFile :)方法做出的UIImage,在DocumentDirectory但方法返回nil图像存在。

  let img = UIImage(contentsOfFile: ChatUtils.getPathWithFolderName(kChatAttachmentOriginalFolderName) + ("/" + fileName))


文章来源: Memory Problems with UIImage(contentsOfFile)