我的图片不显示在我UICollectionView。我不知道什么是错误的。下面是我的代码。
MyCollectionView.swift
/** -- **/
overide func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCollectionViewCell", forIndexPath: indexPath) as! MyCollectionViewCell
let event = events[indexPath.row]
cell.imageView?.image = UIImage(named: "blabla_iPad@2x")
return cell
}
/** -- **/
MyCollectionViewCell.swift
import UIKit
class MyCollectionViewCell: UICollectionViewCell {
var imageView: UIImageView!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
imageView = UIImageView(frame: CGRect(x: 0, y: 16, width: frame.size.width, height: frame.size.height))
imageView.contentMode = UIViewContentMode.ScaleAspectFit
contentView.addSubview(imageView)
}
}
即使我使用了localPath,它仍然没有工作。
/*
/Users/MNurdin/Library/Developer/CoreSimulator/Devices/B30917DD-1C57-430F-9ACF-6526B226CF15/data/Containers/Data/Application/35ACB17A-4C25-4A49-BE3D-04AA51F36CC0/tmp/jreGfApABajWPCnkfPBh.jpg
*/
cell.imageView?.image = UIImage(data: NSData(contentsOfURL: NSURL(fileURLWithPath:localPath)!)!)
var imageView: UIImageView!
这应该是一个IBOutlet。
@IBOutlet weak var imageView: UIImageView!
并将其连接到你的故事板。
其相当的让人摸不到头脑。我不知道到底什么是错在这里,但这里有一些提示,我尝试自己找到发生了什么
后:
?cell.imageView =图像配的UIImage(命名为: “blabla_iPad @ 2X”)
print(cell.imageView?.image.bounds.size)
cell.imageView?.layer.borderWidth = 1.0
cell.imageView?.layer.borderColor = UIColor.blackColor().CGColor
ü将看到的,现在这些东西:1,和以前一样,这意味着ü[R绘制图像细胞外。尝试改变框架。 2.黑色边框空方:指东西是错误的图像打印3帧大小为零。分配的ImageView帧等于图像的边界。
你刚才提到你是从你的故事板加载你的细胞。当从故事板加载时,init(帧:的CGRect)在MyCollectionViewCell类的方法不会被调用,这意味着你的代码,以自定义的UIImageView添加到单元格的内容查看不会被调用了。
因此,这个问题是不是与图像不装入的UIImageView做的,但更多的是与没有获得加入细胞,在所有的整个的UIImageView。
尝试移动是在awakeFromNib()代替
我遇到过同样的问题。只有发生在厦门国际银行文件,我在厦门国际银行在任何的UIImageView切换到斯威夫特3.我已禁用剪辑到bounds后打开,我可以再次看到图像,但现在我的cornerRadius设置不起作用。
基于在awakeFromNib视图大小设置cornerRadius时也有一个问题,因为厦门国际银行的看法与默认的(1000,1000)的帧大小。