【问题标题】:I want to load image Using method of NSURLConnectionDelegate and NSURLConnectionDataDelegate我想加载图像使用 NSURLConnectionDelegate 和 NSURLConnectionDataDelegate 的方法
【发布时间】:2019-02-13 10:58:47
【问题描述】:

我想将图像加载到 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)
    {

    }
}*

【问题讨论】:

  • 您帖子的格式有点不对劲。也许你可以解决这个问题?并提供一些上下文,例如您正在使用的编程语言或环境?另外,请从您的代码示例中删除无意义的注释行。

标签: nsurlconnection


【解决方案1】:

我不熟悉 NSURLConnection 的 Swift 语法,因此我很有可能遗漏了委托方法名称的一些微妙之处,但我突然想到了几个更大的问题:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2016-04-07
    • 2014-08-27
    • 1970-01-01
    相关资源
    最近更新 更多