在图像显示滑块上显示来自api的图像

时间:2017-04-26 06:40:14

标签: xcode swift3 alamofire slideshow

我下载了this库,我的目标是从Jason获取图片链接,然后显示它们。

这是如何通过互联网随机链接来实现的

 let alamofireSource = [AlamofireSource(urlString:
 "https://images.unsplash.com/photo-1432679963831-2dab49187847?w=1080")!, 
AlamofireSource(urlString: "https://images.unsplash.com/photo-1447746249824-    
4be4e1b76d66?w=1080")!,  
AlamofireSource(urlString: 
"https://images.unsplash.com/photo-1463595373836-6e0b0a8ee322?w=1080")!]

我认为我应该做的是:

1-给我的api打电话请求

2-保存图像中的图像

循环遍历数组并在[AlamofireSource(urlString: "variable from the array")!

中包含变量

这是正确的方式还是有更好的方法,即使不使用这个库?

更新

  var Pics = [JSON]()

open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

@IBOutlet var slideshow: ImageSlideshow!

override func viewWillAppear(_ animated: Bool) {
    let url = "http://localhost:8000/api/hello"

    Alamofire.request(url).responseJSON { response in


        if let value: AnyObject = response.result.value as AnyObject? {
            //Handle the results as JSON
            let json = JSON(value)


            for (key, subJson) in json["pic"] {

                self.Pics.append(subJson) as? String

            }
            print(self.Pics)


        }
    }


}


 let kingfisherSource = [KingfisherSource(urlString: Pics)!] // error 


override func viewDidLoad() {
    super.viewDidLoad()

    slideshow.backgroundColor = UIColor.white
    slideshow.slideshowInterval = 5.0
    slideshow.pageControlPosition = PageControlPosition.underScrollView
    slideshow.pageControl.currentPageIndicatorTintColor = UIColor.lightGray
    slideshow.pageControl.pageIndicatorTintColor = UIColor.black
    slideshow.contentScaleMode = UIViewContentMode.scaleAspectFill
    slideshow.currentPageChanged = { page in
        print("current page:", page)
    }

注意:

这是json数据的格式

[
"http://localhost:8000/images/1490104055.jpg",
"http://localhost:8000/images/1490104055.jpg",
"http://localhost:8000/images/1490104055.jpg",
"http://localhost:8000/images/1490104055.jpg",
"http://localhost:8000/images/1490104055.jpg",
null,
null
]

谢谢

0 个答案:

没有答案