代码之家  ›  专栏  ›  技术社区  ›  YungCheng Su

TableView单元格显示带有dispatch_async的图像,显示“void函数中意外的非void返回值”问题

  •  -1
  • YungCheng Su  · 技术社区  · 8 年前

    我想在TableViewCell中显示图像。代码如下:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cellIdentifier = "myCell"
            let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! DIYSquareALLCell
            cell.titles!.text = titles[indexPath.row]
            cell.leftImages!.image = getPic(leftImages[indexPath.row])
            return cell
        }
    
    func getPic(PicURL: String) -> UIImage! {
            let image = self.imageCache[PicURL] as UIImage?
            if image == nil {
                let url = NSURL(string: PicURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!)
                if let data = NSData(contentsOfURL: url!) {
                    imageCache[PicURL] = UIImage(data: data)
                    return UIImage(data: data)!
                }
            } else {
                return image
            }
            return nil
        }
    

    但是滚动TableView非常滞后,所以我更改了函数并在其中添加了一些dispatch_async特性。

    它在我的getPic函数中显示了“void函数中意外的非void返回值”问题。

    在我更改后,代码如下:

    func getPic(PicURL: String) -> UIImage! {
            let image = self.imageCache[PicURL] as UIImage?
            if image == nil {
                let url = NSURL(string: PicURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!)
                let priority = DISPATCH_QUEUE_PRIORITY_HIGH
                dispatch_async(dispatch_get_global_queue(priority, 0)) {
                    let data = NSData(contentsOfURL: url!)
                    if data != nil {
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            self.imageCache[PicURL] = UIImage(data: data!)
                            return UIImage(data: data!)// here is the issue
                        })
                    }
                }
            } else {
                return image
            }
            return nil
        }
    

    谁能告诉我怎么修理它?谢谢

    1 回复  |  直到 8 年前
        1
  •  0
  •   Kumar KL ganeshIOS    8 年前

    使用异步任务时,不能返回值r对象。该函数在主线程上运行,它不会等待异步任务完成。

    让我们用 Closure .

    你的代码应该是这样的:

     typealias CompletionHandler = (image: UIImage) -> Void
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell: testCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! testCell
            downloadFileFromURL(NSURL(string: "http://img.youtube.com/vi/PCwL3-hkKrg/sddefault.jpg")!, completionHandler:{(img) in
                 dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    cell.imgView.image = img
                 })
    
                })
            return cell
        }
    
        func downloadFileFromURL(url1: NSURL?,completionHandler: CompletionHandler) {
            // download code.
            if let url = url1{
            let priority = DISPATCH_QUEUE_PRIORITY_HIGH
            dispatch_async(dispatch_get_global_queue(priority, 0)) {
                let data = NSData(contentsOfURL: url)
                if data != nil {
                    print("image downloaded")
                    completionHandler(image: UIImage(data: data!)!)
                }
                }
            }
        }
    

    示例项目已上载到 GIT .