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