【发布时间】:2020-04-23 09:34:45
【问题描述】:
我有一个 UITextView 并在其中附加了 TextAttachments。我正在从 web url 获取照片。代码如下:
image.load(url: URL(string: ("http://www.furkan.webofisin.com/upload/media/5db1b96366cd8.jpg")))
imageAttachment.image = image.image
let image1String = NSAttributedString(attachment: imageAttachment)
fullString.append(image1String)
这是作为扩展的加载函数:
var imageUrlString: String?
func load(urlString: String) {
imageUrlString = urlString
guard let url = URL(string: urlString) else {
DispatchQueue.main.async {
let urlMessage = "Hata"
appDelegate.errorView(message: urlMessage, color: smoothGreenColor)
}
return
}
image = nil
if let imageFromCache = imageCache.object(forKey: urlString as NSString) {
self.image = imageFromCache
return
}
URLSession.shared.dataTask(with: url) { (data , response, error) in
if error != nil {
DispatchQueue.main.async {
let urlMessage = "Bir hata meydana geldi."
appDelegate.errorView(message: urlMessage, color: smoothGreenColor)
}
return
}
DispatchQueue.main.async {
let imageToCache = UIImage(data: data!)
if self.imageUrlString == urlString {
self.image = imageToCache
}
imageCache.setObject(imageToCache ?? UIImage(named: "astast")!, forKey: urlString as NSString)
}
}.resume()
}
由于缓存,刷新页面后第一次不显示图像。 我检查了 image.image 作为 UIImage 但在加载完成之前它总是为零。
这是第一次打开的截图。
At first opening image data is nil
如何在图像数据填充后检测和重新加载图像。
【问题讨论】:
标签: swift image asynchronous uitextview nstextattachment