Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在从服务器检索的文本之间显示图像[SWIFT]_Ios_Swift_Image_Nstextattachment - Fatal编程技术网

Ios 在从服务器检索的文本之间显示图像[SWIFT]

Ios 在从服务器检索的文本之间显示图像[SWIFT],ios,swift,image,nstextattachment,Ios,Swift,Image,Nstextattachment,我想使用NSTextAttachment在文本之间显示图像,其中文本和图像都是从服务器检索的。我已经托管了要显示的文本的示例JSON。图像需要显示的位置在文本之间用“[image]”标记,要显示的图像链接在JSON中也可用 for para in newsText{ if(para == "[IMAGE]") { print("Image Found") let paraImageLink = images[self.imageCount]["li

我想使用NSTextAttachment在文本之间显示图像,其中文本和图像都是从服务器检索的。我已经托管了要显示的文本的示例JSON。图像需要显示的位置在文本之间用“[image]”标记,要显示的图像链接在JSON中也可用

for para in newsText{
    if(para == "[IMAGE]")
    {
        print("Image Found")
        let paraImageLink = images[self.imageCount]["link"] as? String


        let downloader = ImageDownloader.default
        downloader.downloadImage(with: URL(string:paraImageLink!)!, options: .none) { result in
            switch result {
            case .success(let value): let paraImageResized = self.ResizeImage(value.image, targetSize: CGSize(width: 500.0, height: 300.0))
            let imageAttachement = NSTextAttachment()
            imageAttachement.image = paraImageResized
            let imageString = NSAttributedString(attachment: imageAttachement)
            self.content.append(imageString)
            self.content.append(NSMutableAttributedString(string: "\n\n"))
            self.imageCount+=1

            case .failure(let error):
                print(error)
            }
        }
        //
    }
    else{
        self.content.append(NSAttributedString(string: para+"\n\n"))


    }


我面临的问题是根本没有显示图像

完整代码和示例项目可从以下网址获得:

用于检索数据的JSON位于:

您可以在图像调低后执行此操作

DispatchQueue.main.async {
   self.text.attributedText = self.content
}

我认为这是一个网络形象

 let imageURL : URL = URL(string: url)!
开发人员很少直接从
url
获取图像。它是同步的,挂起主线程

我们通常使用
URLSession
下载它,在后台线程中解码,然后在主线程中呈现

这是一个很好的选择



中,是否有必要使用NSTEXT附件。为什么?你能使用其他工具吗?首先,如果你创建一个简单的项目并将有问题的代码放入其中,人们可以修改它并更新代码,这将对你有更多的帮助。其次,最好先下载图像,然后使用它们创建属性字符串。最后,只需从internet下载一些资源,不要使用第三方库,制作您自己的助手,这会使您的最终应用更小,运行更快。我已添加了该项目。我尝试了它,但它仍然不会显示文本之间的图像。我试图做的是在文本之间显示图像。我想在加载图像时将图像异步附加到文本之间。我也可以在加载图像时显示图像,注意:仅在TABLEVIEW单元格中
let downloader = ImageDownloader.default
downloader.downloadImage(with: url) { result in
    switch result {
    case .success(let value):
        print(value.image)
    case .failure(let error):
        print(error)
    }
}