2016-04-13 25 views
0

私は私が私のドキュメントディレクトリからいくつかの画像をロードするコレクションビューを持っている:UIImageとメモリの問題(contentsOfFile)

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メガバイトによります。

私はUIImage(contentsOfFile)で読み込んだ画像は約150kbです。

[OK]をレットの解決策を試しましたが、それでも成功しませんでした。コントローラーをリロードすると(モーダル)、私のメモリーは常に約30〜40Mバイト増加します。

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(contentsOfFile:....)の代わりにUIImage(named:..)を使用すると、最初のinitでメモリが約40Mバイト増加しますが、Viewを終了してロードすると、もう一度(これは名前付きのために起こると思います:.. ..画像をキャッシュします)

しかし、これはまた、VCを却下してもメモリを解放しません。

+0

メモリにロードされません。必要なのは、URL /パスを持つ配列です –

+0

だから、私はファイル名だけを格納して、次に画像をロードしなければならないということを意味しています。cellForItemAtIndexPath?しかし、なぜViewControllerを却下したときに私のフォトアレイが解放されないのですか? – derdida

+0

はいセルを準備するときにイメージを取得する –

答えて

0

[OK]を私は、以下の溶液で今問題を修正:

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にリサイズした画像を示しています。私はまだこのように、メモリにUIImageを作成

。コントローラーで

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

とを(そうでなければ、ディスク上のサムネイルを保存する必要があります)。

今、メモリ消費量がかなり少なく、ViewControllerを終了すると、使用されたメモリが解放されます。

0

ドキュメントディレクトリにイメージがあります。UIImage(contentsOfFile :)メソッドを使用してUIImageを作成しています。イメージはDocumentDirectoryにありますが、メソッドはnilを返します。

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