Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Image async for NSTextAttachment for UITableViewCell

Loading images dynamically in async thread or image cache library like SDwebimage. Below code is what I tried and it doesn't repaint after image fetched from network.

    let mutableAttributedString = NSMutableAttributedString()

    if let _img = newsItem.img {
        var attachment = NSTextAttachment()
        attachment.bounds = CGRectMake(4, 4, expectedWidth, expectedWidth * _img.ratio)

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            attachment.image = UIImage(data: NSData(contentsOfURL: NSURL(string: _img.src)!)!)
        })

        mutableAttributedString.appendAttributedString(NSAttributedString(attachment: attachment))
    }
like image 474
com.iavian Avatar asked Jan 31 '15 22:01

com.iavian


1 Answers

After image of NSTextAttachment was set, you need to force refreshing of textView contents. For that you can use textView.layoutManager's method invalidateDisplayCharacterRange, where range - is the range of your NSTextAttachement substring

like image 175
KaterinaPetrova Avatar answered Oct 05 '22 22:10

KaterinaPetrova