swift - Loading image to Image Cell from URL -
i'm learning code in swift. i'm trying load image onto image cell located within image well.i'm using following code
import cocoa class viewcontroller: nsviewcontroller { @iboutlet weak var well: nsimagecell! @ibaction func clickme(_ sender: any) { let image = nsimage(byreferencing:nsurl(string: "http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg")! url) well.image=image; } override func viewdidload() { super.viewdidload() // additional setup after loading view. } override var representedobject: any? { didset { // update view, if loaded. } } } no exception thrown nor image loaded. i'm doing wrong?
try this.
simple way.
let url = nsurl(string:"your url") let imagedata = nsdata.init(contentsof: url! url) if imagedata != nil { imageview.image = uiimage(data:imagedata! data) } create extension.
extension uiimageview{ func setimagefromurl(imageurl: string){ if let url = nsurl(string: imageurl) { if let imagedata = nsdata(contentsof: url url) { self.image = uiimage(data: imagedata data) } } } } //use extension imageview.setimagefromurl(imageurl: "your url") use afnetworking.
//without placeholder imageview.setimagewith(url.init(string: "your url")!) //with placeholder imageview.setimagewith(url.init(string: "your url")!, placeholderimage: uiimage.init(named: "placeholder.png")) use sdwebimage.
imageview.sd_setimage(with: url(string: "your url"), placeholderimage: uiimage(named: "placeholder.png"))
Comments
Post a Comment