1

I am drawing a triangle on a UIView via drawRect and subsequently add this UIView as subview of the main view (UIView).

Very easy and it shows.

The problem occurs when I use an UIImageView.

UIView (main view) adding UIImageView as subview and with thisUIImageView adding the UIView as subview onto which I draw the triangle.

Hierarchy:

[UIView] (main view; i.e. self.view)
      |
    [UIImageView] 
            |
         [UIView] (onto which drawing is)

View creations:

self.view = [[UIView alloc] initWithFrame:(CGRect){{0.0, 0.0}, 320.0, 480.0}];
imageView = [[UIImageView alloc] initWithImage:...];

[self.view addSubview:imageView];

viewWithTriangle = [[MyView alloc] initWithFrame:imageView.bounds];
[imageView addSubview:viewWithTriangle];

The triangle is flipped vertically. The easy fix would be to flip the UIView vertically again, but I would like to know why adding the UIView as subview of UIImageView would cause this flip.

This doesn't occur if I add the view where the drawing is directly as subview of the main view.

Unheilig
  • 16,196
  • 193
  • 68
  • 98

1 Answers1

0

The UIImage's orientation likely bleeds into UIImageView's coordinate system at times. UIImageView is highly optimized to work with drawing UIImage's to the screen. UIImageView doesn't even invoke drawRect, for e.g.

Using a breakpoint, when you see this behavior, check the value of imageView.image.imageOrientation. Is it equal to anything other than UIImageOrientationUp? If so, you likely need to work with the fact that the UIImageView is "translating" some properties into the UIImage's "human preferred" coordinate system, instead of the image data's actual coodinate system.

I haven't personally tested this code or approach, as I do not add subviews to UIImageView's, but it's a likely problem. Please let us know if this helps diagnose it -- I'm very curious!

greymouser
  • 3,133
  • 19
  • 22
  • Definitely; shall report back once I tested it. Thanks. – Unheilig Apr 09 '14 at 19:28
  • Do you know a way to get to this meta data properties of the image? Also, can you tell me a bit more about the human preferred coordinate system? Thanks. – Unheilig Apr 11 '14 at 12:51
  • If you just need the orientation, use `imageView.image.imageOrientation`. Getting all EXIF data is straightforward, but non-trivial (look up docs for `CGImageSourceCopyPropertiesAtIndex` and what you need to setup and following to extract the info). I made up "human coordinate system" -- all I really meant was some coordinate system that has a 0,0 point somewhere known, and "up is up, and down is down" in relation the the actual phone/screen/view. If UIImageView's coordinate system is changed by it's loaded UIImage w/ non-up orientation, then human-up *might* be image-left-or-right. *Might*. – greymouser Apr 11 '14 at 13:04