UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 77: Images on real iPhone are distorted

Forums > 100 Days of SwiftUI

Hi everybody

I've been implementing the challenge for day 77 where we have to use the UIImagePickerController to select images for persons.

The ImagePicker implementation is "normal" => as in project 13/Instafilter.

To display the images, I use

Text(person.name)
    .font(.largeTitle)
Image(uiImage: person.image)
    .resizable()
    .scaledToFit()
    .padding()

While playing in the simulator I didn't find issues, but on a real iPhone I have many photos which are distorted. E.g. the aspect ratio is not respected/retained, even though I specified "scale to fit". I have also tried to use .aspectRatio(contentMode: .fit) but this doesn't help either.

I'm not sure which images are affected by this, because I do have a few portrait images which are shown in the correct aspect ratio... but then there are many others where the portrait is not respected and they end up distorted.

Any idea what the source of the problem can be?

5      

I'm having the same problem, but only on images taken with my wife's iPhone 7; images taken with my iPhone XS do not have this issue. It doesn't matter where I use the images, when I open them in Instafilter or the day 77 challenge app on my phone or the simulator, I have the same problem. It's something about the images themselves. At first, I thought it was an issue with the HEIC format or something to do with HDR, but I disabled those settings on her phone and still have the issue. I made a trivial change (auto adjusted levels) to one of the images with preview, and it no longer had the issue. It's something about the way the iPhone 7 (iOS 13.4.1, same as my XS) is saving the images.

3      

OK, so I isolated the problem a little more and came up with a work-around....

Bug: This appears to be a SwiftUI bug with .aspectRatio(contentMode: .fit) and .scaledToFit() that only occurs A) with certain images (e.g. taken with an iPhone 7, etc) and B) when the problematic images are converted from a UIImage to a SwiftUI Image. When the same problematic images are read into a SwiftUI image directly from a file, the problem does not occur.

Work-Around: Specify the size of the original UIImage as the aspect ratio for the SwiftUI image. E.g. given a UIImage named uiImage...

Image(uiImage: uiImage)
  .resizable()
  .aspectRatio(uiImage.size, contentMode: .fit)

5      

I had this issue on my iPhone 11 Pro... so it is not related to "old phones". I had the problem on various images. And today when I tested it again: I haven't found a single image with the problem !?! Is it possible that Apple fixed it now with an update?

I also had crashes with the crystallize filter... they are gone now.

Probably fixed with the latest Xcode 11.4.1

3      

I had the same issue on Xcode 11.6 and also 12.0 beta. I followed @cameter suggestion of using uiImage.size and it fixed the problem. Appreciated!

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.