问题
I've managed to reference images for my custom data model which is used to show unique cells their different information. I've been able to do it for images, as it's a lot easier, I just have no clue how to do it for videos. My data model looks like this:
fileprivate let data = [
CustomData(title: "Test", image: #imageLiteral(resourceName: "ss-1"), url: "this-will-be-a-local-url-in-cache-to-a-video"),
CustomData(title: "Test2", image: #imageLiteral(resourceName: "done-button"), url: "this-will-be-a-local-url-in-cache-to-a-video"),
CustomData(title: "Test2", image: #imageLiteral(resourceName: "notificationIcon"), url: "this-will-be-a-local-url-in-cache-to-a-video")
]
and some of the most relevant code for the assigning the different information per cell in the scrollview:
class CustomCell: UICollectionViewCell{
var data: CustomData?{
didSet{
guard let data = data else { return }
bg.image = data.image
}
}
fileprivate let bg: UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "splash_icon")
iv.translatesAutoresizingMaskIntoConstraints = false
iv.contentMode = .scaleAspectFill
iv.clipsToBounds = true
iv.layer.cornerRadius = 10
return iv
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(bg)
bg.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
bg.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
bg.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
bg.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
Now I know I need the same function as I used for images:
fileprivate let bg: UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "splash_icon")
iv.translatesAutoresizingMaskIntoConstraints = false
iv.contentMode = .scaleAspectFill
iv.clipsToBounds = true
iv.layer.cornerRadius = 10
return iv
}()
Like this for videos, I just don't know how to reference the url some way in an AVPlayer function and reference it in the same way I did in my CustomData class:
fileprivate let video: AVURLAsset = {
}()
Thanks, please comment if you'd like more info, I've tired searching for what AVPlayer function allows me to do something like (AVPlayer.url = "URL") to assign the avplayer with the right url from the data model but obviously it's not that simple ahaha.
Nathan
EDIT:
What I'm asking is how would I achieve the same thing as I did in this image function:
fileprivate let bg: UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "splash_icon")
iv.translatesAutoresizingMaskIntoConstraints = false
iv.contentMode = .scaleAspectFill
iv.clipsToBounds = true
iv.layer.cornerRadius = 10
return iv
}()
so I can reference it in the custom data class:
var data: CustomData?{
didSet{
guard let data = data else { return }
bg.image = data.image
}
}
So it can unwrap the data from the custom data array for me to init it and show it in the view using this function:
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(bg)
bg.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
bg.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
bg.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
bg.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
来源:https://stackoverflow.com/questions/59594656/how-to-reference-video-in-custom-data-model-for-avplayer