图片不显示在我的斯威夫特UICollectionView了

问题描述 投票:1回答:5

我的图片不显示在我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)!)!)
ios swift nsdata nsurl
5个回答
0
投票
 var imageView: UIImageView!

这应该是一个IBOutlet。

@IBOutlet weak var imageView: UIImageView!

并将其连接到你的故事板。


0
投票

其相当的让人摸不到头脑。我不知道到底什么是错在这里,但这里有一些提示,我尝试自己找到发生了什么

后:

?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帧等于图像的边界。


0
投票
  • 你肯定iamge是Xcassets或顶部bundle.Save你的形象“ImageName〜ipad.png”。这仅仅是一个标识符到iOS上使用的i-垫这一形象。把图像中Xcassets。一切都将正常工作即可。
  • 而且还提出了一些日志消息在您的自定义单元格类初始化检查控制流,什么是不对您的自定义类。
  • 你已经注册你的自定义类,以用于在viewDidLoad中该小区标识可以用吗?

0
投票

你刚才提到你是从你的故事板加载你的细胞。当从故事板加载时,init(帧:的CGRect)在MyCollectionViewCell类的方法不会被调用,这意味着你的代码,以自定义的UIImageView添加到单元格的内容查看不会被调用了。

因此,这个问题是不是与图像不装入的UIImageView做的,但更多的是与没有获得加入细胞,在所有的整个的UIImageView。

尝试移动是在awakeFromNib()代替


0
投票

我遇到过同样的问题。只有发生在厦门国际银行文件,我在厦门国际银行在任何的UIImageView切换到斯威夫特3.我已禁用剪辑到bounds后打开,我可以再次看到图像,但现在我的cornerRadius设置不起作用。

基于在awakeFromNib视图大小设置cornerRadius时也有一个问题,因为厦门国际银行的看法与默认的(1000,1000)的帧大小。

© www.soinside.com 2019 - 2024. All rights reserved.