2016-05-24 244 views
0

問題:重裝上陣在UICollectionViewCell緩存的UIImageView(滾動 UICollectionViewController之間切換期間)不保持正確的contentMode。保留圖像縱橫比在UICollectionViewCell

我嘗試在UIImageView上使用.sizeToFit(),並在每次加載時重置.contentMode = UIViewContentMode.ScaleAspectFit,但這沒有幫助。

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> MyCollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("myCell", forIndexPath: indexPath) as! MyCollectionViewCell 

    if (indexPath.item < self.the_array?.count){ 

     if (images_cache[self.the_array![indexPath.item].src!] != nil){ 
      // calling this here doesn't work 
      cell.myImage.contentMode = UIViewContentMode.ScaleAspectFit 
      // calling this here doesn't work either 
      cell.myImage.sizeToFit() 
      cell.myImage.image = images_cache[self.the_array![indexPath.item].src!]! as UIImage 
     } 
     else{ 
      if let url = NSURL(string: "https:" + (self.the_array![indexPath.item].src)!), data = NSData(contentsOfURL: url) { 
       let the_image = UIImage(data: data) 
       cell.myImage.image = the_image 
       images_cache[self.the_array![indexPath.item].src!] = the_image! 
      } 
     } 
    } 
    return cell 
} 

回答

0

解決方案:設置的UIImage的基礎上,電池的大小.frame!

cell.myImage.frame = CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height) 

我把那行,我有.sizeToFit()和/或.contentMode現在它正確地保留圖像。