LoginSignup
1
2

More than 5 years have passed since last update.

コピペでできるUIImageView

Posted at

Swift1や2と変わっているところがあってコピペでできないことが多かったのでメモ

do {
    // 読み込むファイルのURLを指定して表示
    let url : NSURL = NSURL(string: "https://hogeimag" + mediaHensuu)!

        // URL を指定して実際にファイルを NSData として読み込みます。
        let imageData : NSData = try NSData(contentsOf:url as URL,options: NSData.ReadingOptions.mappedIfSafe)
        // 読み込んだ NSData を UIImage へ変換します。
        let img : UIImage! = UIImage(data:imageData as Data)
        //imageViewに画像を表示
        imageView.image = img

} catch {
    print("failed create¥ image")
}

オプションの説明
NSData.ReadingOptions
mappedIfSafe: 安全で可能なら、ファイルを仮想メモリに入れる。
uncached:
alwaysMapped:
https://developer.apple.com/reference/foundation/nsdata.readingoptions

非同期で読み込むにはSDWebImageなどを使うと楽です。

//画像の幅
imageWidth = img.size.width
//画像の高さ
imageHeight = img.size.height

1
2
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
1
2