swift - How to reference video in custom data model for AVPlayer
问题描述
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
}
解决方案
推荐阅读
- google-colaboratory - 有没有办法在 colab 上运行 ipopt
- c# - Discord Bot C# (D#) 无法将 ctx.Member 移动到新创建的语音通道中
- java - 无法解析最新的 Quarkus 插件
- indexing - 如何制作一个使用索引和匹配函数来显示多个响应的 Excel 公式?
- html - 如何将 td 元素放在 h1 元素旁边?
- mongodb - mongodb如何仅选择所有数组值与数据一致的数据
- c# - Asp.Net Core 3.0 授权和认证
- javascript - 将jQuery插件添加到vue组件:未捕获的ReferenceError:未定义行
- multithreading - Golang 睡眠线程而不是忙于等待
- python - 如何改变命名元组?