LoginSignup
8
6

More than 5 years have passed since last update.

Swift初心者メモ web上の画像を表示したい

Last updated at Posted at 2015-08-26

webのサーバー上の画像をURL指定して表示させたいとき
(http://hoge.com/images/hogehoge.png) みたいな

hoge

// UIImageViewを配置
@IBOutlet weak var myImageView: UIImageView!

override func viewDidLoad() {

        super.viewDidLoad()

        // URLオブジェクトを作る
        var imgUrl = NSURL(string: "http://free-illustrations-ls01.gatag.net/images/lgi01b201405240000.jpg");

        // ファイルデータを作る
        var file = NSData(contentsOfURL: imgUrl!);

        // イメージデータを作る
        var img = UIImage(data:file!)

        // イメージビューに表示する
        myImageView.image = img
}

パンダだ!できた

スクリーンショット 2015-08-26 16.51.24.png

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6