打印声明未打印

时间:2018-10-20 05:53:04

标签: swift xcode alamofire arkit alamofireimage

为什么在下面的代码中,AlamofireImage下载图像后,viewWillAppear块中的打印语句会跳过viewWillAppear块中的其余代码...

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)


    Alamofire.request("https://example.com/four.png").responseImage { response in
        debugPrint(response)

        print(response.request as Any)
        print(response.response as Any)
        debugPrint(response.result)

        if let image = response.result.value {
            print("image downloaded: \(image)")

            self.imageServer.append(image)

            print("ImageServer append Successful")
            print("The new number of images = \(self.imageServer.count)")

        }
    }


///////////THESE STATEMENTS ARE BEING SKIPPED/////////////////
    print("The new number of images = \(imageServer.count)")
    print("Test")
    trackedImages = loadedImagesFromDirectoryContents(imageServer)
    configuration.trackingImages = trackedImages
    configuration.maximumNumberOfTrackedImages = 1
    sceneView.session.run(configuration)
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用completionHandler解决此问题。 function完成后将执行该程序。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    fetchImage {
        print("The new number of images = \(imageServer.count)")
        trackedImages = loadedImagesFromDirectoryContents(imageServer)
        configuration.trackingImages = trackedImages
        configuration.maximumNumberOfTrackedImages = 1
        sceneView.session.run(configuration)
    }

}

func fetchImage(completion: @escaping ()->()) {
    Alamofire.request("https://example.com/four.png").responseImage { response in
        debugPrint(response)

        print(response.request as Any)
        print(response.response as Any)
        debugPrint(response.result)

        if let image = response.result.value {
            print("image downloaded: \(image)")

            self.imageServer.append(image)

            print("ImageServer append Successful")
            print("The new number of images = \(self.imageServer.count)")

        }
        completion()
    }
}
相关问题