Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: load an image from url

People also ask

How do I download an image from a URL?

Click on the Download Image from URL button, the field will appear on the right. Enter the full web address of the image. Click on the arrow to the right of the field and select the Force Check checkbox. Then click the Save button.

How do I fetch an image in Swift?

Select New > Project... from Xcode's File menu and choose the Single View App template from the iOS > Application section. Name the project Images and set User Interface to Storyboard. Leave the checkboxes at the bottom unchecked. Tell Xcode where you would like to save the project and click the Create button.


Just use the size property of UIImage, for example:

NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
CGSize size = img.size;

In swift:

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

In swift regarding using optionals:

var url:NSURL? = NSURL(string: imageString)
var data:NSData? = NSData(contentsOfURL : url!)
var image = UIImage(data : data!)

IN SWIFT 3.0

The main thread must be always remain free so it serves the user interface and user interactions.

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()
}

}

To download Asynchronous image with 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 image with default 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
                       }
                    }
                }
            }
        }
    }