swift - Showing image from url with placeholder in UITextView with attributed string -
i'm developing lib markdown swift , works fine! there little problem, when load images in markdown put placeholder image , dispatch process load original image, this:
var attachment: nstextattachment = nstextattachment() attachment.image = uiimage(named: "placeholder.png") attachment.bounds = cgrect(origin: cgpointzero, size: attachment.image.size.width > uiscreen.mainscreen().bounds.width ? cgsizemake(uiscreen.mainscreen().bounds.width, uiscreen.mainscreen().bounds.width/attachment.image.size.width * attachment.image.size.height) : attachment.image.size ) dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), { () -> void in let imagedata: nsdata = nsdata(contentsofurl: url) attachment.image = uiimage(data: imagedata) attachment.bounds = cgrect(origin: cgpointzero, size: attachment.image.size.width > uiscreen.mainscreen().bounds.width ? cgsizemake(uiscreen.mainscreen().bounds.width, uiscreen.mainscreen().bounds.width/attachment.image.size.width * attachment.image.size.height) : attachment.image.size ) self.textview.setneedsdisplay() }) let attributedstring: nsattributedstring = nsattributedstring(attachment: attachment) let linebreak: nsattributedstring = nsattributedstring(string: "\n") self.markdown.deletecharactersinrange(matchrange) self.markdown.insertattributedstring(attributedstring, atindex: matchrange.location) self.markdown.insertattributedstring(linebreak, atindex: matchrange.location)
but after loading image place holder doesn't disappear until user start scroll, correct image shown!
so interested know what's problem?
you're trying change ui global queue, might not work. may need use main queue this.
dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), { () -> void in let imagedata: nsdata = nsdata(contentsofurl: url) let image = uiimage(data: imagedata) dispatch_async(dispatch_get_main_queue()) { // on main queue (the main thread) attachment.image = image attachment.bounds = cgrect(origin: cgpointzero, size: attachment.image.size.width > uiscreen.mainscreen().bounds.width ? cgsizemake(uiscreen.mainscreen().bounds.width, uiscreen.mainscreen().bounds.width/attachment.image.size.width * attachment.image.size.height) : attachment.image.size ) self.textview.setneedsdisplay() } })
Comments
Post a Comment