gpt4 book ai didi

ios - UITableView 中的 Swift 异步调用

转载 作者:行者123 更新时间:2023-11-30 11:39:46 25 4
gpt4 key购买 nike

异步调用UIImage加载到textView作为tableView中的NSTextAttachment的最佳方法是什么?到目前为止,这效果非常糟糕。

我使用 URL 字符串在多个 tableView 单元格内加载单个图像。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

//Transform Data From ^ to load at the bottom
tableView.transform = CGAffineTransform (scaleX: 1,y: -1);
cell?.contentView.transform = CGAffineTransform (scaleX: 1,y: -1);
cell?.accessoryView?.transform = CGAffineTransform (scaleX: 1,y: -1);

let username = cell?.viewWithTag(1) as! UITextView
username.text = messageArray[indexPath.row].username

let message = cell?.viewWithTag(2) as! UITextView
//message.text = messageArray[indexPath.row].message // delete later

var test = messageArray[indexPath.row].uploadedPhotoUrl
print(test ?? String.self)

if(test != ""){
// create an NSMutableAttributedString that we'll append everything to
let fullString = NSMutableAttributedString(string: "")

// create our NSTextAttachment
let image1Attachment = NSTextAttachment()

URLSession.shared.dataTask(with: NSURL(string: messageArray[indexPath.row].uploadedPhotoUrl)! as URL, completionHandler: { (data, response, error) -> Void in
if error != nil {
print(error ?? String())
return
}

DispatchQueue.main.async(execute: { () -> Void in
let image = UIImage(data: data!)
image1Attachment.image = image

//calculate new size. (-20 because I want to have a litle space on the right of picture)
let newImageWidth = (message.bounds.size.width - 20 )

//resize this
image1Attachment.bounds = CGRect.init(x: 0, y: 0, width: newImageWidth, height: 200)

// wrap the attachment in its own attributed string so we can append it
let image1String = NSAttributedString(attachment: image1Attachment)

// add the NSTextAttachment wrapper to our full string, then add some more text.
fullString.append(image1String)
fullString.append(NSAttributedString(string: message.text))

// draw the result in a label
message.attributedText = fullString

//message.textStorage.insert(image1String, at: message.selectedRange.location)

message.textColor = .white

test = ""
})
}).resume()
}else {
message.text = messageArray[indexPath.row].message
}

let timeStamp = cell?.viewWithTag(3) as! UILabel
timeStamp.text = messageArray[indexPath.row].timeStamp

let imageView = cell?.viewWithTag(4) as! UIImageView
imageView.image = nil
let urlString = messageArray[indexPath.row].photoUrl
imageView.layer.cornerRadius = 10
imageView.clipsToBounds = true

//Load profile image(on cell) with URL & Alamofire Library
let downloadURL = NSURL(string: urlString!)
imageView.af_setImage(withURL: downloadURL! as URL)

return cell!
}

当索引滚动(出现和消失)时,图像仍然滞后,并且也没有完全加载

enter image description here

最佳答案

当 tableviewcell 滚动时,您一次加载一张图像。有一些时间调用服务然后等待响应返回,从而影响滚动,尽管它在另一个线程上。

您可以尝试批量调用图像,一次可能为 10 或 20 个。

关于ios - UITableView 中的 Swift 异步调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49375647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com