Ios 在UICollectionView中将图像添加到AvPlayServiceController
根据UICollectionView的indexPath,我正在尝试向AvPlayServiceController contentOverlayView添加一个图像 我希望应用程序根据UICollectioView indexPath播放具有特定背景图像的音频。当用户选择特定indexPath的项目时,我从API接收音频文件和图像,并使用SDWebImage库从URL下载图像 以下是我的collectionView应在indexPath代码中选择项:Ios 在UICollectionView中将图像添加到AvPlayServiceController,ios,swift,uicollectionview,avfoundation,sdwebimage,Ios,Swift,Uicollectionview,Avfoundation,Sdwebimage,根据UICollectionView的indexPath,我正在尝试向AvPlayServiceController contentOverlayView添加一个图像 我希望应用程序根据UICollectioView indexPath播放具有特定背景图像的音频。当用户选择特定indexPath的项目时,我从API接收音频文件和图像,并使用SDWebImage库从URL下载图像 以下是我的collectionView应在indexPath代码中选择项: override func collect
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
}
非常感谢您的帮助。您正在使用一个返回图像本身的完成块。所以你只需要在那里设置图像
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)
})
}
我已经尝试过你的答案,但是AVPlayerServiceController overlay视图上仍然没有图像我以前没有使用过sdwebimage,但是从文档中我可以看到它下载图像并将其设置为异步。在您的情况下,imageView是在sdWebImage设置图像之前返回的。因此,您需要添加一个调度组,以便仅在图像视图下载完毕后返回图像视图,并且可以帮助我创建调度组吗?我认为您最好使用一个完成块。查看我的更新。我已尝试了你的代码,但图像未显示。我为显示帧=(0;0)的图像制作了一个print语句。