如何在UITextView内部动态加载图像

问题描述 投票:0回答:1

我有一个UITextView并在其中添加了TextAttachments。我正在从网址获取照片。这是代码:

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,但在加载完成之前始终为nil。

这是第一次打开的屏幕截图。

At first opening image data is nil

如何在填充图像数据后检测并重新加载图像。

swift image asynchronous uitextview nstextattachment
1个回答
0
投票

添加完成作为最初获取图像的调用是异步的,与本地缓存不同

func load(urlString: String,completion:@escaping(()  -> () )) {

    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
        completion()
        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)

            completion()
       }

    }.resume()

}

通话

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)

}
© www.soinside.com 2019 - 2024. All rights reserved.