Swift TableViewController和自定义UITableViewCell之间的关系
在我的Swift TableViewController和自定义UITableViewCell之间的关系,swift,swift2,Swift,Swift2,在我的tableviewcell中,我有一个UIImageView。此ImageView用于向用户显示录制视频的缩略图。下面是我如何设置ImageView @IBOutlet weak var thumbImageView: UIImageView! { didSet { let videoUrl = NSURL(string: Link to Video ) var thumbImage: UIImage? l
tableviewcell
中,我有一个UIImageView
。此ImageView
用于向用户显示录制视频的缩略图。下面是我如何设置ImageView
@IBOutlet weak var thumbImageView: UIImageView! {
didSet {
let videoUrl = NSURL(string: Link to Video )
var thumbImage: UIImage?
let asset = AVAsset(URL: videoUrl!)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMake(asset.duration.value / 3, asset.duration.timescale)
if let cgImage = try? assetImgGenerate.copyCGImageAtTime(time, actualTime: nil) {
thumbImage = UIImage(CGImage: cgImage)
print("Got Thumb")
self.thumbImageView.image = UIImage(CGImage: cgImage)
}
}
}
在我的TableViewCell
中,我还有:
var URL: String? {
didSet {
print(URL!)
}
}
在我的TableView
中,我调用了Parse数据库,在那里我存储了用户保存在我的S3服务器上的视频链接。在cellforrowatinexpath
方法中,我使用PFObject为我的URL变量提供URL:
cell.URL = Some URL
我想要的工作是:
let videoURL = NSURL(string: self.URL!)
但是当我使用传递给thumbImageView的URL运行代码时,我得到了零。有没有其他方法可以让我设置这个
感谢您的帮助 您想将您的
didSet
代码从thumbImageView
移动到URL
属性,这样每当URL发生变化时,它就会加载图像
目前,您的代码不会在正确的时间运行,因为只有当从故事板/xib加载单元格时才会设置
imageView
。设置thumbImageView出口时,并不意味着您已完全初始化,因此无法引用自己。这就是你得到零分的原因。请阅读swift。如此简单的解决方案!非常感谢。