ios - 如何在 swift 中使用滑块向 UIImageView 显示多个图像 url 字符串
问题描述
我是 swift 新手,我面临向 uiimage 显示多个 url 字符串图像的问题
我的代码是这样的
在 viewdidLoad
Array = [UIImage (named: "ISDI1")!,UIImage (named: "ISDI2")!,UIImage (named: "ISDI3")!,UIImage (named: "ISDI4")!,UIImage (named: "ISDI5")!]
sliderView.maximumValue = Float(Array.count - 1)
@IBAction func btnPreviousClick(_ sender: Any) {
sliderView.value -= 1
imgView.image = Array[Int(sliderView.value)]
}
@IBAction func btnNextClick(_ sender: Any) {
sliderView.value += 1
imgView.image = Array[Int(sliderView.value)]
}
@IBAction func SliderViewClick(_ sender: UISlider) {
var value = Int(sender.value)
imgView.image = Array[value]
}
现在我得到这样的url字符串
http://diceapp.in/mailer_isdi_2020/images/4.jpeg , http://diceapp.in/mailer_isdi_2020/images/3.jpeg , http://diceapp.in/mailer_isdi_2020/images/1.jpeg
如何在图像视图中显示此网址?
我正在使用此代码将 url 转换为图像,但它现在崩溃了
let url = NSURL(string:Image ?? "")
if !(url?.absoluteString == "") {
let data = NSData(contentsOf: url! as URL) //make sure your image in this url does exist, otherwise unwrap in a if let check
imgView.image = UIImage(data: data! as Data)
}
解决方案
首先,您需要使用此字符串从字符串中获取图像 url
var imageUrlStrings = "http://diceapp.in/mailer_isdi_2020/images/4.jpeg , http://diceapp.in/mailer_isdi_2020/images/3.jpeg , http://diceapp.in/mailer_isdi_2020/images/1.jpeg"
var imageArr = imageUrlStrings.components(separatedBy: " , ")
的值imageArr
将像
["http://diceapp.in/mailer_isdi_2020/images/4.jpeg", "http://diceapp.in/mailer_isdi_2020/images/3.jpeg", "http://diceapp.in/mailer_isdi_2020/images/1.jpeg"]
现在您有了单独的图像 URL。下载并在单个索引中显示图像。
推荐阅读
- java - Maven构建依赖问题
- react-native - 如何在场景加载时禁用 iOS 反弹效果?
- javascript - 在 asp.net 网络表单中使用 Sweet 警报删除行
- java - 在 mockito 中使用时模拟 ContextLoader.getCurrentWebApplicationContext() 调用。我该怎么做?
- c# - 如何在我的模块中使用 BinaryConnection 发送和接收数据
- git - 获取合并提交的合并分支名称
- shell - 用于从 S3 存储桶获取所有文件的 Shell 脚本
- aws-lambda - AppSync 与 Kinesis 的集成
- python - 不区分大小写的文件名模式匹配返回无
- css - 为什么当浏览器窗口缩小时我的弹性项目没有缩小?