Swift 3 Playground URLSession异步中的UIImage Gray

时间:2017-03-08 02:56:10

标签: ios swift swift-playground

Bach显示在put()文件中,但显示为灰色。切换不同的.playground没有帮助。

enter image description here

Result Display Modes

使用Swift 3.0在import UIKit import PlaygroundSupport PlaygroundPage.current.needsIndefiniteExecution = true URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil) var theImage = UIImageView() let urlNew = NSURL(string: "https://upload.wikimedia.org/wikipedia/commons/6/6a/Johann_Sebastian_Bach.jpg") let task = URLSession.shared.dataTask(with: urlNew! as URL) { (data, response, error) -> Void in if error != nil { print("thers an error in the log") } else { DispatchQueue.main.async() { let image = UIImage(data: data!) theImage.image = image } } } task.resume() 中呈现Bach或任何UIImageView的最佳方法是什么?

1 个答案:

答案 0 :(得分:3)

我找出了你所看到的问题,并在这里创建了一个游乐场来说明:https://github.com/dtweston/Bach.playground

import UIKit
import PlaygroundSupport

// Toggle the code below to fade the image out or display it properly
//PlaygroundPage.current.needsIndefiniteExecution = true

let image = UIImage(named: "Johann_Sebastian_Bach.jpg")

基本上,当游乐场仍在运行时,图像会显得褪色。由于您needsInfiniteExecution设置为true,因此它总是显得褪色。