2
votes

I'm using UIGestureRecognizers to rotate and resize an image using uiimageview. Now problem is that I need to get the new size of the image from UIImageView transform (which is CGAffineTransform) property. I found this code at StackOverFlow, but it is not returning proper values. As imageview is not only resized but also rotated. So my question is how to get the new size and location from the rotated and resized UIImageView's transform property?

CGRect newRect = CGRectApplyAffineTransform(self.imageView.bounds, self.imageView.transform);

Anyone can help?

Thanks

ANSWER OK, I found a fix. The main thing creating the problem in size calculation was the rotation. So what a did check it here.

 CGFloat angle = atan2(self.imageView.transform.b, self.imageView.transform.a);
 CGAffineTransform tempTransform = CGAffineTransformRotate(self.imageView.transform, -angle);  
 CGRect newRect = CGRectApplyAffineTransform(CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height), tempTransform);

Thanks everybody

2

2 Answers

1
votes

You can use temp points to simulate transforms of the size's dimensions, something like this:

CG_INLINE CGSize FAE_CGSizeCalcDimentionsAfterApplyTransformation(CGSize size, CGAffineTransform transform)
{
    CGPoint widthPoint = CGPointMake(size.width, 0.0);
    CGPoint heightPoint = CGPointMake(0.0, size.height);

    CGPoint centerAfterTransfrom = CGPointApplyAffineTransform(CGPointZero, transform);
    CGPoint widthAfterTransform = CGPointApplyAffineTransform(widthPoint, transform);
    CGPoint heightAfterTransform = CGPointApplyAffineTransform(heightPoint, transform);

    CGSize sizeAfterTransformation = CGSizeMake(TB_CGPointCalcDistance(centerAfterTransfrom, widthAfterTransform), TB_CGPointCalcDistance(centerAfterTransfrom, heightAfterTransform));

    return sizeAfterTransformation;
}
0
votes

From the Apple doc:

Because affine transforms do not preserve rectangles in general, the function CGRectApplyAffineTransform returns the smallest rectangle that contains the transformed corner points of the rect parameter.

Maybe you should try this: CGSizeApplyAffineTransform:

Returns the height and width resulting from a transformation of an existing height and width.

Try to use the old size of the imageView:

CGSize oldImageSize = imageView.bounds.size;

// Transforming code here

CGAffineTransform currentTransform = imageView.transform;
CGSize newImageSize = CGSizeApplyAffineTransform(oldImageSize, currentTransform);