【问题标题】:Return value from completion handler - Swift完成处理程序的返回值 - Swift
【发布时间】:2015-07-24 10:35:10
【问题描述】:

我在 Utilities 类中使用 loadImage 方法,但在通过闭包返回图像时遇到了一些问题。基本上是因为我的代码可能返回图像或错误,因此在调用该方法时将其分配给图像属性将不起作用。

我在类的方法声明中使用的方法是否错误,或者我应该以不同的方式调用该方法以预测可能不同的结果?谢谢

public class UtilitiesService: NSObject {
    public class func loadImage(urlString:String)
    {

    var imgURL: NSURL = NSURL(string: urlString)!
    let request: NSURLRequest = NSURLRequest(URL: imgURL)
    NSURLConnection.sendAsynchronousRequest(
        request, queue: NSOperationQueue.mainQueue(),
        completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
            if error == nil {
                self.returnImage(data)
            }
    })
}

public class func returnImage(imageData: NSData) -> UIImage {

    return UIImage(data: imageData)!

}
}

//// view controller
class someView: UIViewController {
var image.image = loadImage(url) ///will throw a return type error
 }

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    为您的 loadImage 函数添加一个处理程序:

    斯威夫特 3

     func loadImage(_ urlString: String, handler:@escaping (_ image:UIImage?)-> Void)
        {
    
            let imageURL: URL = URL(string: urlString)!
    
            URLSession.shared.dataTask(with: imageURL) { (data, _, _) in
                if let data = data{
                    handler(UIImage(data: data))
                }
            }.resume()
        }
    

    这样调用函数:

    loadImage("SomeURL") { (image) -> Void in
                if let image = image{
                    DispatchQueue.main.async {
                        self.imageView.image = image
                    }
                }
            }
    

    斯威夫特 2.3

    func loadImage(urlString: String, handler: (image:UIImage?)-> Void)
        {
    
            let imageURL: NSURL = NSURL(string: urlString)!
    
            NSURLSession.sharedSession().dataTaskWithURL(imageURL) { (data, _, _) in
                if let data = data{
                    handler(image: UIImage(data: data))
                }
                }.resume()
        }
    

    这样调用函数:

      loadImage("someURL") { (image) -> Void in
                if let image = image{
                    dispatch_async(dispatch_get_main_queue()) {
                        self.imageView.image = image
                    }
                }
            }
    

    【讨论】:

      【解决方案2】:

      我会这样做:

      public class UtilitiesService: NSObject {
          public class func loadImage(urlString:String, completion:(resultImage:UIImage) -> Void)
          {
      
          var imgURL: NSURL = NSURL(string: urlString)!
          let request: NSURLRequest = NSURLRequest(URL: imgURL)
          NSURLConnection.sendAsynchronousRequest(
              request, queue: NSOperationQueue.mainQueue(),
              completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
                  if error == nil {
                      completion(resultImage: UIImage(data: data))
                  }
          })
      }
      }
      
      //// view controller
      class someView: UIViewController {
      var image: UIImageView()? //Create the variable the way you need
      loadImage(url, completion: { (resultImage) -> Void in
          image = resultImage //Assign the result to the variable
      })
       }
      

      我认为这会起作用,如果不起作用,请告诉我,我会解决它

      【讨论】:

      • 谢谢!但是得到:不能将类型 () 的值分配给 UIImage 类型的值?
      • 哦,我没看到,等一下
      猜你喜欢
      • 2016-06-02
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      相关资源
      最近更新 更多