One step affine transform for rotation around a point?

后端 未结 4 785
有刺的猬
有刺的猬 2020-11-27 03:13

How can I make a Core Graphics affine transform for rotation around a point x,y of angle a, using only a single call to CGAffineTransformMake() plus math.h trig functions su

4条回答
  •  难免孤独
    2020-11-27 04:11

    For those like me, that are struggling in search of a complete solution to rotate an image and scale it properly, in order to fill the containing frame, after a couple of hours this is the most complete and flawless solution that I have obtained.

    The trick here is to translate the reference point, before any trasformation involved (both scale and rotation). After that, you have to concatenate the two transform in order to obtain a complete affine transform.

    I have packed the whole solution in a CIFilter subclass that you can gist here.

    Following the relevant part of code:

    CGFloat a = _inputDegree.floatValue;
    CGFloat x = _inputImage.extent.size.width/2.0;
    CGFloat y = _inputImage.extent.size.height/2.0;
    
    CGFloat scale = [self calculateScaleForAngle:GLKMathRadiansToDegrees(a)];
    
    CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
    transform = CGAffineTransformRotate(transform, a);
    transform = CGAffineTransformTranslate(transform,-x,-y);
    
    
    CGAffineTransform transform2 = CGAffineTransformMakeTranslation(x, y);
    transform2 = CGAffineTransformScale(transform2, scale, scale);
    transform2 = CGAffineTransformTranslate(transform2,-x,-y);
    
    CGAffineTransform concate   = CGAffineTransformConcat(transform2, transform);
    

提交回复
热议问题