Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift中使用url为视图设置背景图像_Ios_Swift_Api_View_Background Image - Fatal编程技术网

Ios 如何在swift中使用url为视图设置背景图像

Ios 如何在swift中使用url为视图设置背景图像,ios,swift,api,view,background-image,Ios,Swift,Api,View,Background Image,我正在使用swift 2.2开发一个应用程序在我的应用程序中,我需要将一个背景图像添加到视图中,我知道如何将其添加到imageview,但我无法将其添加到视图中。直接将图像设置为UIView。首先,您需要将image设置为ImageView,并将该ImageView作为addSubView添加到UIView。如果您想要异步加载映像(为了更好的性能),可以使用Alamofire的代码,如下所示 Alamofire.download("https://httpbin.org/image/png").

我正在使用swift 2.2开发一个应用程序在我的应用程序中,我需要将一个背景图像添加到视图中,我知道如何将其添加到imageview,但我无法将其添加到视图中。

直接将图像设置为
UIView
。首先,您需要将image设置为
ImageView
,并将该
ImageView
作为addSubView添加到
UIView
。如果您想要异步加载映像(为了更好的性能),可以使用Alamofire的代码,如下所示

Alamofire.download("https://httpbin.org/image/png").responseData { response in
   if let data = response.result.value {
      let image = UIImage(data: data)
   }
}
这是我的工作

 let url = NSURL(string: "https://httpbin.org/image/png")
    let data = NSData(contentsOfURL: url!)
    let image = UIImage(data: data!)
    self.view.layer.contents = image?.CGImage 

将图像作为子视图添加到该视图,或在UIView的背景上绘制图像:这有助于添加默认图像,但我需要添加图像,您可以使用异步库下载并设置图像:或任何其他简单的idea@rajkumar你能分享一些代码吗?您需要传入视图而不是图像视图向我显示无法调用下载的错误,参数列表为stringNo ways,我们无法将图像设置为UIView。将url.absoluteString作为参数传递给该方法是的,只有我在尝试它时才给出错误,如图所示,错误为无法使用参数列表字符串调用下载…am使用swift 2.2而不是swift 3am获取错误,因为无法使用参数列表字符串调用下载am使用swift 2。2@rajkumar你用什么样的方法在项目中下载图像?你用那种方法更新了你的问题吗?我刚按照你的回答回答了。我也有alamofire框架,但不知道为什么会这样error@rajkumar你在let image=UIImage(data:data)中得到图像了吗?@rajkumar现在我做了更改,检查它在我的项目中的工作是否正常
 let url = NSURL(string: "https://httpbin.org/image/png")
    let data = NSData(contentsOfURL: url!)
    let image = UIImage(data: data!)
    self.view.layer.contents = image?.CGImage