如何使函数在swift中返回它的内部闭包值

时间:2017-08-22 01:21:47

标签: ios swift

如何从downloadedImage返回beginGetImageRequest

func beginGetImageRequest() {

    if let imagePath = thumbPath {
        request = Alamofire.request(.GET, imagePath).response(completionHandler: { (_, _, imageData, error) -> Void in
            if error != nil {
                NSLog("Error downloading thumbnail image: \(error)")
            } else {
                if let downloadedImage = UIImage(data: imageData!) {
                    self.imageView.image = downloadedImage
                }
            }
        })
    }
}

1 个答案:

答案 0 :(得分:0)

误解了请求,我将其编辑为agian:

func beginGetImageRequest(completion:(_ image:UIImage)->()){

    if let imagePath = thumbPath {
        request = Alamofire.request(.GET, imagePath).response(completionHandler: { (_, _, imageData, error) -> Void in
            if error != nil {
                NSLog("Error downloading thumbnail image: \(error)")
            } else {
                if let downloadedImage = UIImage(data: imageData!) {
                    completion(downloadedImage)
                }
            }
        })
    }
}

调用方法时:

beginGetImageRequest { (image:UIImage) in
        // code you need
    }