Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving all 4 coordinates of a rotated UIImageView

How does one get the 4 coordinates for a UIImageView?

I know the CGRect can be obtained and the origin.x and origin.y, but how can all 4 corners be found?

EDIT: I am rotating the UIImageViews, thats why I asked :P

like image 984
some_id Avatar asked Feb 24 '23 15:02

some_id


2 Answers

You could add width and height of the rectangle to get the coordinates of the other 3 points.

CGRect rect = view.bounds;

CGPoint topLeft = rect.origin;
CGPoint topRight = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y);
CGPoint bottomLeft =CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
CGPoint bottomRight = CGPointMake(rect.origin.x + rect.size.width,
                                  rect.origin.y + rect.size.height);

Then you could use CGPointApplyAffineTransform to get the transformed coordinates of them under your specified transform.

CGPoint center = view.center;
CGAffineTransform transf = CGAffineTransformMakeTranslation(-rect.size.width/2,
                                                            -rect.size.height/2);
transf = CGAffineTransformConcat(transf, view.transform);
transf = CGAffineTransformTranslate(transf, center.x, center.y);

topLeft = CGPointApplyAffineTransform(topLeft, transf);
//...

(note: not tested.)

like image 101
kennytm Avatar answered Feb 27 '23 04:02

kennytm


This is my solution:

  • [self] is a subclass of UIImageView
  • [self.transform] is the transform i make on [self]:

    CGAffineTransform transform = CGAffineTransformMakeTranslation(-center.x, -center.y);
    transform = CGAffineTransformConcat(transform, self.transform);
    CGAffineTransform transform1 = CGAffineTransformMakeTranslation(center.x, center.y);
    transform = CGAffineTransformConcat(transform, transform1);
    
    CGPoint leftTopPoint     = CGPointApplyAffineTransform(leftTopPoint, transform);
    CGPoint rightTopPoint    = CGPointApplyAffineTransform(rightTopPoint, transform);
    CGPoint rightBottomPoint = CGPointApplyAffineTransform(rightBottomPoint, transform);
    CGPoint leftBottomPoint  = CGPointApplyAffineTransform(leftBottomPoint, transform);
    
like image 45
RayChen Avatar answered Feb 27 '23 05:02

RayChen