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の空判定をする

Last updated at Posted at 2023-09-08

今回は、UIImageを空判定する方法で「こんな判定の仕方があったんだぁ」と思ったので書き留めておきます。
文字列の空判定を行うisEmptyのUIImage版です。
※ ちなみにUIImageにisEmptyは存在しません。

前提として中身がUIImage()だったときの判定方法です!


例1

let image = UIImage()

if image.size == CGSize.zero {
    // 中身が空のときの処理
    print("空")
} else {
    // 中身が空じゃない時の処理
    print("空じゃない")
}

例2

if image.size.width == 0 || image.size.height == 0 {
    // 画像が空の場合の処理
    print("空")
} else {
    // それ以外の場合の処理
    print("空じゃない")
}

例3

if image.size.equalTo(.zero) {
    // 画像が空の場合の処理
    print("空")
} else {
    // 空じゃない
    print("空じゃない")
}

まとめ

こんな感じで画像の中身のsizeが0だった判定すれば空判定ができるはず

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?