问题: UICollectionViewCell中重新加载的缓存UIImageView(在滚动或在UICollectionViewController之间切换时)不保留正确的contentMode。
我尝试在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 :(得分:0)
解决方案:根据单元格的大小设置UIImage的.frame!
cell.myImage.frame = CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height)
我将该行放在.sizeToFit()和/或.contentMode的位置,现在它可以正确保存图像。