1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Swift]UIImageのサイズを変更する(Extensionは使わない)

Last updated at Posted at 2022-06-22

はじめに

UIImageのsizeを変更しようとすると、Cannot assign to property: 'size' is a get-only propertyとエラーが発生します。対処法を調べてみると、UIImageを拡張することで対応しているコードが多いと思います。

ただ、Extensionは使わずにUIImageのsizeを変更したいと思う人もいるはずなので、今回はそんな人向けにコードを紹介します。

コード

ContentView.swift
func hogehoge() {
    let image = UIImage(named: "画像名")
    let scaledImageSize = CGSize(width: 100, height: 30)
    let renderer = UIGraphicsImageRenderer(size: scaledImageSize)
    let scaleImage = renderer.image { _ in
        image.draw(in: CGRect(origin: .zero, size: scaledImageSize)
    }
    
    let imageView = UIImageView()
    imageView.image = scaleImage
}

以上!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?