如何在页面视图控制器中的所有视图控制器中使用Api下载图片?

时间:2017-03-01 12:28:31

标签: ios json api swift3 uipageviewcontroller

我的应用程序中有一个页面视图控制器,这个页面视图控制器有4个视图控制器 - 所以我在所有这些页面中都有Image View View控制器,我想用Json Api在这些页面中下载图像

这是我的代码但是我在所有的视图控制器类中复制了这段代码,所以我只在这里写一个

varBlankCount = Application.WorksheetFunction.CountIf(Range("A:A"), "*" & Chr(160) & "*")

**这会起作用但不完全正确**

这是我的故事板的图片

as you see in the picture I have Page VC2 that is page view controller and four view controllers that connected to the page VC2

1 个答案:

答案 0 :(得分:0)

您拥有多个VC的事实与下载图像的任务无关。

然而,除了缺乏抽象之外,你走在正确的轨道上:使用URLSessionURLSessionTask是使用Apple API的正确方法。

您可能对以下内容感兴趣;抽象的东西很多开发人员使用这个名为Alamofire的第三方库,特别是你的AlamofireImage

例如,您需要使用AlamofireImage

let imageView = UIImageView(frame: frame)
let url = URL(string: "https://httpbin.org/image/png")!
let placeholderImage = UIImage(named: "placeholder")!

imageView.af_setImage(withURL: url, placeholderImage: placeholderImage)