我想使用NSURLConnectionDelegate和NSURLConnectionDataDelegate的方法加载图像

时间:2019-02-13 10:58:48

标签: nsurlconnection

我想将图像加载到connectionDidFinishLoading方法中,并且函数func connection(_connection:NSURLConnection,didReceive:Data)没有被调用

class ImageDownload: UIImageView,NSURLConnectionDelegate,NSURLConnectionDataDelegate
{
    var imageSaved:UIImage!
    var imageDownloaded:UIImage!
    var connection2:NSURLConnection = NSURLConnection()
    var data: NSMutableData = NSMutableData()
    var urlstring:String = ""
    var fileURL:URL!
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray
    var pathPlist:String!

    func downloadImage()
    {
        let imgdownload :String =  "http://image.tmdb.org/t/p/w500" + urlstring
       // let urlnew: NSURL = NSURL(string: imgdownload)!
        //print(urlnew,"url")
        let url: NSURL = NSURL(string: imgdownload)!
         let request1: NSMutableURLRequest = NSMutableURLRequest(url: url as URL)
      //  let request2: NSMutableURLRequest = NSMutableURLRequest(url: urlnew as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 60.0)
        connection2 = NSURLConnection(request: request1 as URLRequest, delegate: self, startImmediately: false)!
        connection2.start()
    }

    func setURL(url:String) -> Void
    {
        print(url,"url")
        urlstring = url
        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        fileURL = documentsDirectory.appendingPathComponent(url)
        print(fileURL,"fileurl")

        if FileManager.default.fileExists(atPath: fileURL.path)
        {
            let image = UIImage(contentsOfFile: fileURL.path)
            print("file exists")
            self.image = image
        }
        else
        {
           downloadImage()
            //let imgdownload :String =  "http://image.tmdb.org/t/p/w500" + url
           // let request = URL(string: imgdownload)
           // let myUrl = NSURL(string: imgdownload)
            //print("image loaded")
           // self.image = self.imageSaved
        }
    }

        func connection(_ connection: NSURLConnection, didReceive response: URLResponse)
        {
            print("in didReceive response\n")
            self.data = NSMutableData()
        }
        func connection(_connection: NSURLConnection, didReceive: Data)
       {
        print("in didReceive data\n")

        self.data.append(data as Data)
        print(data,"image data is")
        }
    func connection(_ connection: NSURLConnection, didFailWithError error: Error)
    {
        print("connection error = \(error)")

    }
    func connectionDidFinishLoading(_ connection: NSURLConnection)
    {

    }
}*

1 个答案:

答案 0 :(得分:0)

我对NSURLConnection的Swift语法不熟悉,所以我不太可能错过一些与委托方法名称有关的细微之处,但我的头上却看到了两个更大的问题:

  • 您为什么在Swift中使用NSURLConnection?每个支持Swift的iOS版本也都有NSURLSession。只需使用共享会话,您的行为将与NSURLConnection几乎相同。
  • 您正在使用HTTP URL。在所有最新版本的iOS中,如果希望应用程序能够访问HTTP URL,则必须在Info.plist中添加特殊位。我不建议这样做。只需使用HTTPS。

您可以从Let's Encrypt获得HTTPS的免费TLS证书。我的猜测是,一旦您设置好并切换到HTTPS URL,即使使用现有代码,问题也将消失。但是您仍然应该使用NSURLSession。