gpt4 book ai didi

ios - 将图像添加到 UICollectionView 中的 AVPlayerViewController

转载 作者:行者123 更新时间:2023-11-30 11:19:49 25 4
gpt4 key购买 nike

我正在尝试根据 UICollectionView 的 indexPath 将图像添加到 AVPlayerViewController contentOverlayView。

当用户选择特定索引路径处的项目时,我希望应用程序根据 UICollectioView indexPath 播放具有特定背景图像的音频,我正在从 API 接收音频文件和图像,并且正在使用 SDWebImage 库下载来自 URL 的图像。

这是我的 collectionView shouldSelectItemAt indexPath 代码:

override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {

SVProgressHUD.show()
if let Url = URL(string: musicData[indexPath.row].musicCellAudioFile) {
let musicfileURL = Url
let audioPlayer = AVPlayer(url: musicfileURL)
let controller = AVPlayerViewController()
controller.player = audioPlayer
controller.player = audioPlayer
present(controller, animated: true) {

audioPlayer.play()

}
controller.contentOverlayView?.addSubview(imageFromUrl(musicImageUrlString: musicData[indexPath.row].musicImage))

controller.contentOverlayView?.backgroundColor = UIColor(red: 178/255, green: 178/255, blue: 122/255, alpha: 1)



}else{
print("NO music file")
SVProgressHUD.dismiss()
}


return true
}

这是我的 imageFromUrl 方法:

func imageFromUrl(musicImageUrlString:String) -> UIImageView{

let image = UIImage()
let imageView = UIImageView(image: image)
imageView.sd_setImage(with: URL(string: musicImageUrlString), placeholderImage: UIImage(named: "App-Default"),options:SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
SVProgressHUD.dismiss()
})
return imageView


}

非常感谢您的帮助。

最佳答案

您正在使用返回图像本身的完成 block 。所以你只需要在那里设置图像即可。

func imageFromUrl(musicImageUrlString:String) -> UIImageView {

let image = UIImage()
let imageView = UIImageView(image: image)
imageView.sd_setImage(with: URL(string: musicImageUrlString), placeholderImage: UIImage(named: "App-Default"),options:SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
SVProgressHUD.dismiss()
imageView.image = image
})
return imageView
}

更新

override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {

SVProgressHUD.show()

if let Url = URL(string: musicData[indexPath.row].musicCellAudioFile) {
let musicfileURL = Url
let audioPlayer = AVPlayer(url: musicfileURL)
let controller = AVPlayerViewController()
controller.player = audioPlayer
controller.player = audioPlayer
imageFromUrl(musicImageUrlString: "your URL") { (imageView) in
guard let imageView = imageView else { return }
controller.contentOverlayView?.addSubview(imageView)
controller.contentOverlayView?.backgroundColor = UIColor(red: 178/255, green: 178/255, blue: 122/255, alpha: 1)
present(controller, animated: true) {
audioPlayer.play()
}
}
}
else{
print("NO music file")
SVProgressHUD.dismiss()
}

return true
}

还有你的函数

typealias closure = (UIImageView?) -> Void

func imageFromUrl(musicImageUrlString:String, completion: @escaping closure) {
let image = UIImage()
let imageView = UIImageView(image: image)
imageView.sd_setImage(with: URL(string: musicImageUrlString), placeholderImage: UIImage(named: "App-Default"),options:SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
imageView.image = image
completion(imageView)
})
}

关于ios - 将图像添加到 UICollectionView 中的 AVPlayerViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51408917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com