【Swift】UIImageで画像を指定する方法
今回はUIImageで画像を指定する方法をこの記事で説明します。
この記事のポイント・UIImageを使用(Url指定)
画像を指定する方法
UIImageで画像を指定する場合は以下のように設定します。
@IBOutlet weak var imgField: UIImageView!
// Screen Size の取得
screenWidth = self.view.bounds.width
screenHeight = self.view.bounds.height
// img
let bimage:UIImage = UIImage(url: "https://XXXXXXXXX") ← 画像のUrlを指定
// 画像の幅・高さの取得
width = bimage.size.width
height = bimage.size.height
// 画像サイズをスクリーン幅に合わせる
scale = screenWidth / width
let rect:CGRect = CGRect(x:0, y:0, width:width*scale, height:height*scale)
self.imgField.frame = rect
// 画像の中心をスクリーンの中心位置に設定
self.imgField.center = CGPoint(x:screenWidth/2, y:screenHeight/2)
self.imgField.image = bimage
これで画像が表示されます。