1
0

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] 単色の CIImage を生成する方法

Last updated at Posted at 2023-05-13

概要

単色の CIImage を生成したい、という場面に遭遇したことがあったので、その際の知見を共有したいと思います。

使うもの

これだけです。

やり方

CIImage(color: someCIColor)

「ってやればいいんやろ、なんでこんなことで記事書くの?w」 って思ったそこのあなた、ここがハマりポイントです。

初期化しただけではサイズが 0 x 0 の画像が表示されてしまいます。
(extent.size を print してみると、CGSize(width: 0, height: 0) が出力される)

なので、例えば 1920 x 1280 の単色の画像を生成したい場合は、

CIImage(color: someCIColor)
    .cropped(
        to: CGRect(
             origin: .zero,
             size: CGSize(
                 width: 1920,
                 height: 1280
             )
        )
    )

という風にクロップしてやる必要があります。不思議ですね👀
(extent.size は .zero になってるけど実は .infinity な大きさなのかな?)

余談

UIGraphicsImageRenderer を使って単色の UIImage を生成する方法もありますが、これは上記のやり方と比べると生成に要する時間が長いです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?