简体   繁体   中英

View is flipped when being added as subview of UIImageView

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 this UIImageView 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.

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 eg

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM