logo
TOOLS TO HELP YOU LEARN
teachers, students, everyone
Tools | Blog | Account | Learn
Login Status

Display Image From Web URL
How to download an image from a webserver and display it in your app
Jan 17, 2017

Create a new single view project and copy the code below into your ViewController.swift

Otherwise, you're ready to run the code. (see full code below). Enjoy!

  •                   
                      
                     
    import UIKit
    
    class ViewController: UIViewController {
        
        var playButton:UIButton?
        var myImage:UIImageView?
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            myImage = UIImageView(frame: CGRect(x:0, y:0, width:200, height:200))
            myImage?.center = self.view.center
            
            // Do any additional setup after loading the view, typically from a nib.
            playButton = UIButton(type: UIButtonType.system) as UIButton
            playButton!.frame = CGRect(x: 25, y: 60, width: 110, height: 35)
            playButton!.backgroundColor = UIColor.lightGray
            playButton!.setTitle("Play", for: UIControlState.normal)
            // playButton!.setImage(UIImage(named: "play.png"), for: UIControlState.normal)
            playButton!.tintColor = UIColor.black
            playButton!.addTarget(self, action: #selector(ViewController.playButtonTapped(_:)), for: .touchUpInside)
            self.view.addSubview(playButton!)
        }
    
      
        func playButtonTapped(_ sender:UIButton)
        {
            // nextDownload()
        }
        
        
        func nextDownload(){
            let imageUrlString = "https://dl.dropboxusercontent.com/u/2813968/trump.png"
            let imageUrl:URL = URL(string: imageUrlString)!
            
            // Start background thread so that image loading does not make app unresponsive
            DispatchQueue.global(qos: .userInitiated).async {
                
                let imageData:NSData = NSData(contentsOf: imageUrl)!
                
                // When from background thread, UI needs to be updated on main_queue
                DispatchQueue.main.async {
                    let image = UIImage(data: imageData as Data)
                  //   let croppedImage = self.cropImage(screenshot: image!)
                 //  self.myImage?.image = croppedImage;
                 self.myImage?.image = image
                   self.myImage?.contentMode = UIViewContentMode.scaleAspectFit
                    self.view.addSubview(self.myImage!)
                }
            }
        }
        
        func cropImage(screenshot: UIImage) -> UIImage {
            
            let crop = CGRect(x: 50, y: 50, width: 50, height: 50)
            
            let cgImage = screenshot.cgImage!.cropping(to: crop)
            let image: UIImage = UIImage(cgImage: cgImage!)
            
            
            return image
        }
        
        func cropImage2(screenshot: UIImage) -> UIImage {
            
            let crop = CGRect(x: 50, y: 50, width: 50, height: 50)
            
            let cgImage = screenshot.cgImage!.cropping(to: crop)
            let image: UIImage = UIImage(cgImage: cgImage!)
            
            
            return image
        }
        }
                      
                      

    Comments:




    No comments posted

  • blog

    Past Posts

    Dropbox Public Folder Alternative
    Jun 4, 2017

    Getting Started with NodeJS
    May 1, 2017

    Scratch Intro: Racing Game
    Jan 15, 2017

    Five Best Educational Tools For Teachers
    Feb 3, 2017

    Coding Gravity
    Feb 22, 2017

    Drones in Eduction
    March 2, 2017

    Javascript Intro
    April 1, 2017

    iPhone Streaming Audio App
    April 15, 2017

    Installing MySQL
    May 15, 2017

     

     


     

    Thank You!