Why is UIImage rotated after being initialised from asssets? - ios

I have an image in my xcassets folder which is in PDF format and I init and assign to an UIImageView this way:
imageView.image = UIImage(named: "myImage", in: Bundle.current, compatibleWith: nil)?.withRenderingMode(.alwaysTemplate)
When I run the app, this image is displayed rotated/flip, but I'm not rotating it in code. The image file looks ok. What could it be happening there?

I had the same issue which is completely ununderstandable. But it only happened to a tiny rectangular vector image oriented vertically, placed in xcassets. I tried a few things like changing all the settings xcasset gives us. Alas, the only thing that worked for me was to increase the image size. Then iOS stopped changing the orientation.
It really looks like a bug from xcode trying to compress/optimize images and mistakenly rotate images...
Anyway, try to change the image size (scale it up - or insert margins). Also, try to export to another media type.

Related

scale type or contentMode of Image in UIButton is not working

I was trying to put an image in UIButton and the image would fill up the space keeping the aspect ratio intact. I was not able to apply the content mode. Please see the screenshot
I also tried from the code and it's not working as well
myButton.imageView?.contentMode = .scaleAspectFill
By changing the alignment, the image takes the whole space but it does not keep the aspect ratio and content mode does not work as well.
What's causing your problem is how small the image is. There are two things you can do. One option is to set the image you have now as the background image and then change the button's constraints to get the proportions right. You won't be able to have the button be the size you have it now, though, because the image will be all stretched out.
The other thing you could do is find a bigger or different image of that symbol (square.and.arrow), rather than using the one already loaded into Xcode.

Image looks bad when testing, but original image file is looking just fine

Why does my image look awful when I'm testing the app on my 5s. It also looks awful when testing it on the simulator. My image has the same width as the 5s: 640px. It is saved as #2x. The UIImageView has the exact same size as the image: 640. The UIImageView is also keeping the view in ratio.
My result
I'm trying to archive the same quality result as shown in the second image, which is screenshotted from a random relevant app.
The quality I want
I can't add the original image file because I need 10 reputation for another link. But I can assure you that the original image file looks just fine. So somewhere in Xcode there is going something wrong?
Deleting the line below fixed it:
self.Aktau1?.layer.shouldRasterize = true
Without that line, my code now looks like this (to apply a shadow):
self.Aktau1?.layer.shadowColor = UIColor.blackColor().CGColor
self.Aktau1?.layer.shadowOpacity = 0.15
self.Aktau1?.layer.shadowOffset = CGSizeMake(0, 45
self.Aktau1?.layer.shadowRadius = 25

CLKComplicationTemplateUtilitarianSmallRingImage not showing image

I'm having issues using CLKComplicationTemplateUtilitarianSmallRingImage with a central image.
I'm pretty sure that at one point the image I choose was showing up within the progress ring, but after relauncing a few times, the image disappeared. Oddly, when I've tried to use it in CLKComplicationTemplateUtilitarianSmallSquare and CLKComplicationTemplateUtilitarianSmallFlat the image shows up just fine.
For the life of me I cannot figure out what is going on.
Here is the code I'm using:
let smallRing = CLKComplicationTemplateUtilitarianSmallRingImage()
smallRing.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "Complication/Utilitarian")!)
smallRing.ringStyle = .Closed
smallRing.fillFraction = 5 / 9
handler(CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: smallRing))
A few more details:
Not working on device or simulator.
My image is 14pt (28px), I have tried with PDF (single vector) and PNGs (non-interlaced).
Works as expected with any CLKComplicationTemplateUtilitarianSmall* that accepts images except CLKComplicationTemplateUtilitarianSmallRingImage.
Have tried using a Complication Image Set, just an image in the Catalog both Apple Watch and Universal...
Have tried not setting other properties on CLKComplicationTemplateUtilitarianSmallRingImage.
Crashes on targetting the incorrect image name, so I know it's loading the image when the name is accurate.
Tried with tint color and just changing around the image's default color to see if that's it. Nope. Would think it's the image if it wasn't working in other Templates!
I have to be doing something unusual though.
Looks like this was a bug with WatchOS 2.2.1!
Thankfully fixed in 2.2.2!

UIImage being stretched when copying from UIPasteboard

I have a valid image with the right content size, however I have noticed when I try to paste this image to WhatsApp or through iMessage the image get's significantly stretched. I have looked at the image and it seams to be fine on UIImageview, but somehow it gets stretched in particular to outer controls.
Is there anyway to fix this issue?
When copying an image make sure to modify the imageView's mode. There are different modes, like Aspect Fit, Scale to fill and so on.
If let's say the image is 500x300 and your imageView's size is set to 350x230 and mode to Scale to fill, it will end up stretched. Try to fiddle around and see what works for you. Most of the times i choose Aspect Fit.
Try that out.
BTW. You can find it under inspection tab in design editor or set it programmatically if you choose so.

Achieving desired resolution of UIImage issue

I am trying to create a custom annotationView.
I have a base.png image with the following parameters 60x100 pixels with 72pixels/inch resolution.
In my custom Annotation object I set annotationView's image with the following method:
-(MKAnnotationView *)annotationView
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:#"MyAnnotation"];
annotationView.image = [UIImage imageNamed:#"base.png"];
return annotationView;
}
Everything is working as desired except for resolution of the custom annotationView. It looks like this:
Although, original image looks like this:
As you can see it looks over sized with pixels clearly noticeable. I don't have any front-end experience to speak of and I don't understand much about resolution and how UIImage sets it's image under the hood by calling imageNamed method.
What is the issue here? Do I need a higher resolution image to begin with or different size image or something else??? How does iOS translate the original .png image (it's size, resolution etc.) to the image that is actually displayed on the device?
Have a look at Apple's high resolution resources webpage.
Essentially, there are two parts to this.
Are you using #2x images for retina? You should have a double resolution copy of your image (in your case, 120 x 200) named base#2x.png.
You UIImageView will automatically take the size of its image property unless you specify a frame.
It looks like your pin and your coffee cup are two different images. I'd start by making that base#2x.png image at double resolution. When you write your code, you don't actually specify the #2x version of your image. You code as you did, and iOS will look for the Retina resolution copy and magically load it instead of the standard resolution. You'll need both images, like so:
Make sure to add both of them to your project. In code, you'd write [UIImage imageNamed:#"base.png"] and on retina devices, you'll get the sharper version of the picture.
As for the offset of the coffee on the pin, you need to play with the frame (or constraints, if you're using auto layout) of that view a bit.

Resources