2015-05-13 108 views
1

我遇到了UIImage的問題。我已經手動將UIImageView對象添加到滾動視圖。問題是:當我有超過50張圖片時,內存將增加到200MB左右,並且該應用將在iPhone 4或4s上崩潰。當我收到內存警告以防止崩潰但我不知道如何使用Swift釋放它們時,我想釋放內存以顯示不可見部分中的圖像。請幫幫我。如何釋放UIImageView的內存(Swift)

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

    // Dispose of any resources that can be recreated. 
} 

func loadImage(index:Int){ 
if self.imgPaths.count == 0 { 
    println("Has not data") 
    actInd.stopAnimating() 
    return 
} 
var imgURL: NSURL = NSURL(string: self.imgPaths[index].stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet()))! 
let width:CGFloat = self.view.bounds.width 
let height:CGFloat = self.view.bounds.height 
var view:UIView = UIView(frame: CGRectMake(0, 0, width, height)); 

if let imgObj = self.dicData[index] { 

} 
else 
{ 
println("imgURL \(imgURL)") 
let request: NSURLRequest = NSURLRequest(URL: imgURL) 
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in 
    if error == nil { 
     let imgItem = UIImage(data: data)! 

     var te :Float = self.imgPaths.count > 0 ? Float(index + 1)/Float(self.imgPaths.count) : 1 
     self.progressView.setProgress(te, animated: true) 

     if let imgObj = self.dicData[index] { 

      if index < self.imgPaths.count - 1 
      { 
       var nextIndex:Int = index + 1 
       self.loadImage(nextIndex) 
      } 
      if(index == self.imgPaths.count - 1) 
      { 
       if self.currentImageIndex > 0 
       { 
        self.isAddedFirstImg = true 
       } 
       if !self.isAddedFirstImg 
       { 
        self.scrollViews[0].zoomScale = self.zoomScales[0] 
        self.view.insertSubview(self.scrollViews[0], belowSubview: self.tabBar.viewWithTag(77)!) 
        self.isAddedFirstImg = true 
       } 

       self.actInd.stopAnimating() 
       println("loaded image") 
      } 
     } 
     else 
     { 
      self.dicData[index] = UIImageView(image: imgItem) 

      self.dicData[index]?.frame = CGRect(origin: CGPointMake(0.0, 0.0), size:imgItem.size) 

      // 2 
      self.scrollViews[index].addSubview(self.dicData[index]!) 
      self.scrollViews[index].contentSize = imgItem.size 

      // 3 
      var doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "scrollViewDoubleTapped:") 
      doubleTapRecognizer.numberOfTapsRequired = 2 
      doubleTapRecognizer.numberOfTouchesRequired = 1 
      self.scrollViews[index].addGestureRecognizer(doubleTapRecognizer) 

      var singleTapRecognizer = UITapGestureRecognizer(target: self, action: "scrollViewSingleTapped:") 
      singleTapRecognizer.numberOfTapsRequired = 1 
      singleTapRecognizer.numberOfTouchesRequired = 1 
      self.scrollViews[index].addGestureRecognizer(singleTapRecognizer) 

      var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:") 
      swipeRight.direction = UISwipeGestureRecognizerDirection.Right 
      self.scrollViews[index].addGestureRecognizer(swipeRight) 

      var swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:") 
      swipeLeft.direction = UISwipeGestureRecognizerDirection.Left 
      self.scrollViews[index].addGestureRecognizer(swipeLeft) 

      // 4 
      var scrollViewFrame = self.scrollViews[index].frame 
      var scaleWidth = scrollViewFrame.size.width/self.scrollViews[index].contentSize.width 
      var scaleHeight = scrollViewFrame.size.height/self.scrollViews[index].contentSize.height 
      var minScale = min(scaleWidth, scaleHeight) 
      self.zoomScales[index] = minScale 
      self.scrollViews[index].minimumZoomScale = minScale 

      // 5 
      self.scrollViews[index].maximumZoomScale = 1.0 
      self.scrollViews[index].delegate = self 

      // 6 
      self.centerScrollViewContents(index) 

      dispatch_async(dispatch_get_main_queue(), { 
       println("downloaded image index: \(index) CH.\(self.chapterID)") 
       if(index == 0) 
       { 
        self.scrollViews[0].zoomScale = self.zoomScales[0] 
        self.view.insertSubview(self.scrollViews[0], belowSubview: self.tabBar.viewWithTag(77)!) 
        self.actInd.stopAnimating() 
       } 

       if index < self.imgPaths.count - 1 && !self.stopDownload 
       { 
        var nextIndex:Int = index + 1 
        self.loadImage(nextIndex) 
       } 
       if(index == self.imgPaths.count - 1) 
       { 
        if self.currentImageIndex > 0 
        { 
         self.isAddedFirstImg = true 
        } 
        if !self.isAddedFirstImg 
        { 
         self.scrollViews[0].zoomScale = self.zoomScales[0] 
         self.view.insertSubview(self.scrollViews[0], belowSubview: self.tabBar.viewWithTag(77)!) 
         self.isAddedFirstImg = true 
        } 

        self.actInd.stopAnimating() 
        println("loaded image") 
       } 
      }) 
     } 
    } 
    else { 
     println("Error: \(error.localizedDescription)") 
    } 
}) 
} 

}

+1

如果我是你,我會使用一個collectionView而不是一個scrollView,因爲可見的單元格將被取出以節省內存。您可以在索引路徑中爲行設置單元格中的照片,然後將您的圖片請求分頁。 – Dare

+0

@Dare我也需要放大圖像,所以我使用scrollView。是否有可能放大集合視圖? – user38931

回答

5

夫特使用ARC(自動引用計數)來管理存儲器。要從內存中釋放某些東西,必須刪除對該對象的所有引用。 ARC維護一個引用該對象的位數的計數,當該計數達到0時,釋放該內存。

對於您的情況,您的imageViews存儲在self.dicData[index],它們被添加爲子視圖self.scrollViews[index]。您需要從其超級視圖和self.dicData中刪除imageView。一旦你這樣做,內存將被釋放。

如果你有ImageView的的index要釋放:

self.dicData[index]?.removeFromSuperview() 
self.dicData[index] = nil 
+0

謝謝,它的工作原理 – user38931

0

在迅速有一個叫做「懶」 真正的好工具,你可能想研究關於懶惰的VAR的時候還非常方便直到圖像和桌面視圖(爲您的實例collectionview)

當您使用自定義單元格時,您將看到巨大的差異。 創建圖像視圖,例如懶惰變量imageViewer:UIImageView 我的應用程序在大約150mb 200mb的內存現在它在40〜60mb嘗試它!