ios - 在幻灯片 iOS APP 中从 URL Xcode 9 加载图像
问题描述
大家好,我想知道如何从 URL 加载图像,即使我在 iOS 应用程序中添加了 1、2、3 等
本练习的重点是远程更改图像并在应用程序中实时显示
实际上我可以显示来自 iOS 设备的图像,这是我的代码
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var ImageView: UIImageView!
var timer: Timer!
var counter = 1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.ImageView.image = UIImage(named: "IMAGE_TO_SHOW1")
counter+=1;
timer=Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(animate), userInfo: nil, repeats: false)
}
@objc func animate(){
print("Animate Method")
UIView.transition(with: self.view, duration: 5, options: UIViewAnimationOptions.curveEaseOut,
animations: {
UIView .setAnimationRepeatCount(5)
if(self.counter<6){
let name = "IMAGE_TO_SHOW\(self.counter)"
self.ImageView.image=UIImage(named: name)
self.counter+=1;
Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.animate), userInfo: nil, repeats: false)
};if(self.counter==6){
self.counter=1
}
}, completion: {finished in
})
}
}
但是我只能显示 5 张图像,如果我想要 1 或 2 或 3 或 n 则需要显示 n 数量的图像
例子:
如果我想要 4 张图片然后显示
https://www.images.com/IMAGE_TO_SHOW1
https://www.images.com/IMAGE_TO_SHOW2
https://www.images.com/IMAGE_TO_SHOW3
https://www.images.com/IMAGE_TO_SHOW4
如果我想要 3 张图片然后显示
https://www.images.com/IMAGE_TO_SHOW1
https://www.images.com/IMAGE_TO_SHOW2
https://www.images.com/IMAGE_TO_SHOW3
******编辑******我将解释我需要什么
我有 2 家商店(我自己的服装店)我有 4 台 ipad(每家商店 2 台)我想在每台 iPad 上显示折扣幻灯片 我有一个服务器,我可以在其中发布带有直接 URL 的图像 (www.domain.com/image1 .jpg) 我想通过在我的服务器中上传图像来远程更改幻灯片图像有时我有 2 个折扣,有时我有 5 个折扣
实际上我必须更新应用程序才能显示新图像,但我只想通过将图像上传到服务器来做到这一点如何仅通过更改服务器上的图像来显示幻灯片更新图像?
******编辑******
提前谢谢你的帮助
解决方案
因为您将动画完成处理程序中的计数器重置为:
if(self.counter==6){
self.counter=1
}
而不是 6 根据您的数组添加动态计数(如果您将图像网址保存在数组中)。
if(self.counter==YOUR_IMAGE_URLS_ARRAY_COUNT){
self.counter=1
}
~ 编辑 ~
对于动态加载图像,yoiu 可以使用Kingfisher 库。
该库提供了使用完成处理程序从 url 设置图像的方法。如果您愿意,您可以在完成处理程序中设置动画,或者您可以设置带有动画的图像。
您可以使用的方法:
imageViewBg.kf.setImage(with: , placeholder: , options: , progressBlock: , completionHandler: )
希望能帮助到你