Miloo
Miloo

Reputation: 127

UIimage Detect Landscape Image with Swift

I want to read a photo from Library

in this function

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage

now I want to know is that landscape photo or not I try to tie this code

if(pickedImage?.size.width > pickedImage?.size.height)

But I received this error Binary operator '>' cannot be applied to two 'CGFloat?' operands how can I resolve this Problem

Upvotes: 0

Views: 237

Answers (4)

Shehata Gamal
Shehata Gamal

Reputation: 100503

Try this

 if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage

   { 
      if pickedImage.size.width > pickedImage.size.height 
      {
         /// landscape mode
      }

   }

}

Upvotes: 2

kiwisip
kiwisip

Reputation: 447

Another possible solution could be to use switch pattern matching:

switch image?.size {
case .some(let size) where size.height > size.width:
  // do something
default:
  break
}

Upvotes: 0

Miloo
Miloo

Reputation: 127

I find the solution with this code ? -> !

if(pickedImage!.size.width > pickedImage!.size.height)

Upvotes: 1

pacification
pacification

Reputation: 6018

The problem here that you try to work with Optional values, instead of unwrapped. To unwrap value you can use guard:

guard let pickedImage = pickedImage else { 
    // false logic here
    return 
}

if pickedImage.size.height > pickedImage.size.height {

} else {

}

By the way, are you sure that you want to compare pickedImage's height to itself? Looks weird.

Upvotes: 1

Related Questions