IOS: download image from url

I need to download an image from a URL and set it to a UIImageView; the problem is that I don’t know the exact size of the image, then how can I display the image correctly?

+51
ios uiimage nsurl uiimageview
Jun 10 2018-12-12T00:
source share
7 answers

Just use the UIImage size property, for example:

NSURL *url = [NSURL URLWithString:path]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [[UIImage alloc] initWithData:data]; CGSize size = img.size; 
+114
Jun 10 '12 at 21:40
source share

In quick:

 var url = NSURL.URLWithString("http://www.example.com/picture.png") var data = NSData(contentsOfURL : url) var image = UIImage(data : data) image.size // if you need it 
+14
08 Sep '14 at 18:26
source share

With quick use of options:

 var url:NSURL? = NSURL(string: imageString) var data:NSData? = NSData(contentsOfURL : url!) var image = UIImage(data : data!) 
+9
Jan 07 '15 at 15:32
source share

In SWIFT 3.0

The main thread should always remain free, so it serves the user interface and user interaction.

 class ViewController: UIViewController { @IBOutlet weak var imageView: UIImageView! private func fetchImage() { let imageURL = URL(string: "https://i.stack.imgur.com/9z6nS.png") var image: UIImage? if let url = imageURL { //All network operations has to run on different thread(not on main thread). DispatchQueue.global(qos: .userInitiated).async { let imageData = NSData(contentsOf: url) //All UI operations has to run on main thread. DispatchQueue.main.async { if imageData != nil { image = UIImage(data: imageData as! Data) self.imageView.image = image self.imageView.sizeToFit() } else { image = nil } } } } } override func viewDidLoad() { super.viewDidLoad() fetchImage() } } 
+7
Dec 18 '16 at 11:33
source share

To download an asynchronous image with the Kingfisher library, you can follow this step, url: https://github.com/onevcat/Kingfisher :

  func imageFromUrl(_ urlString: String) { if let url = URL(string: urlString) { ImageDownloader.default.downloadImage(with: url, options: [], progressBlock: nil) { (image, error, url, data) in DispatchQueue.main.async { self.imageView.image = image } } } } 

You can also download the default image URLSession.shared.dataTask

  func imageFromUrl(_ urlString: String) { if let url = URL(string: urlString) { let request = URLRequest(url: url) URLSession.shared.dataTask(with: request) {(data,response,error) in if let imageData = data as Data? { if let img = UIImage(data: imageData){ DispatchQueue.main.async { self.imageView.image = img } } } } } } 
+1
May 23 '17 at 11:49 a.m.
source share

SWIFT 5.0 + background sampling

 private func fetchImage(_ photoURL: URL?) { guard let imageURL = photoURL else { return } DispatchQueue.global(qos: .userInitiated).async { do{ let imageData: Data = try Data(contentsOf: imageURL) DispatchQueue.main.async { let image = UIImage(data: imageData) self.userImageView.image = image self.userImageView.sizeToFit() self.tableView.reloadData() } }catch{ print("Unable to load data: \(error)") } } } 
0
Jun 06 '19 at
source share

One of the common errors when displaying an image downloaded from json or url is the queue problem. EVERYTHING associated with the user interface must be executed in the main queue , so if you forget about it, even the perfect code (the answers above are good) will not display your image. To call mainQueue, use code like this, and note that a call to the main queue may need to be executed separately in the called imageDisplay function:

 dispatch_async(dispatch_get_main_queue(), ^{ self.nameLabel.text = self.pokemon.name; [self displayImage]; //CALLS FUNCTION self.abilitiesTextView.text = @"loves SwiftUI"; }); - (void)displayImage { NSString *imageURLString = [NSString stringWithFormat: self.pokemon.sprite]; NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageURLString]]; dispatch_async(dispatch_get_main_queue(), ^{ self.spriteImageView.image = [UIImage imageWithData: imageData]; }); // NOTE: important for newer versions of XCode/Obj-C... //[imageData release]; With ARC ("automated release..."), release method is forbidden, it already done for you. } 
0
Jul 27 '19 at 20:27
source share



All Articles