0
votes

i have the following situation , i'm trying to make a photo editor and i need to do scale,translate and rotate operations for an image with around center of view

the problem is that if i apply translate after 90 degree translate left is translate top and the anchor point is not the center of the view after translation

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // ImageView
    v = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 768, 500)];
    [v setBackgroundColor:[UIColor redColor]];

    // ImageView's Image
    UIImage *img = [UIImage imageNamed:@"_my.jpg"];
    [v setImage:img];
    v.contentMode = UIViewContentModeScaleAspectFit;
    v.layer.anchorPoint = CGPointMake(0.5, 0.5);
    [self.view setBackgroundColor:[UIColor clearColor]];

    // UiView containing ImageView
    vc = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 500)];
    [vc addSubview:v];
    [self.view addSubview:vc];
}

- (IBAction)Click:(id)sender {
    // Translate
    if ([sender tag] == 1) {
        CGAffineTransform t0 = v.transform;
        CGAffineTransform t1 = CGAffineTransformTranslate(t0, 10.0, 0.0);
        v.transform = t1;
    }
    // Rotate
    if ([sender tag] == 2) {
        CGAffineTransform t0 = v.transform;
        CGAffineTransform t1 = CGAffineTransformRotate(t0, DEGREES_TO_RADIANS(10));
        v.transform = t1;
    }    
}

does anyone know how can i keep the center of view as the anchor point and translate after rotation to work properly ?

tx

1
easier way it's remove translate, apply rotation and then apply translate back en.wikipedia.org/wiki/Transformation_(function)Volodymyr B.
i need translate,scale and rotate actions multiple times in any order, the goal is that user can do anything he want with the image to fit it in a cropping areajohn Stilkowicz
Ok, but anyway it should be has right order of transformation check this cosc.brocku.ca/Offerings/3P98/course/lectures/2d_3d_xformsVolodymyr B.

1 Answers

0
votes

Transformations with Origin

However, especially in 2D, you frequently need to add an origin displacement to your transformation. That's usually added to the beggining, and must be negated too, so:

// World Matrix with Origin

-OriginTranslation * Scale * Rotation * PositionTranslation

The same thing can also be done with a view matrix, but you add it to the end and this time you don't negate it:

// View Matrix with Origin

-PositionTranslation * -Rotation * Zoom * OriginTranslation

Transformations with Selective Origin

There are also some cases where you want to have an origin displacement, but you want it to affect only your scale and rotation, but not your translation. The way to handle that is to undo the origin transformation before reaching the translation component. For example, with the world matrix:

I don't remember, exactly, how transformation is applying for view, one from those should be work.

// World Matrix with Origin for Scale and Rotation

-OriginTranslation * Scale * Rotation * OriginTranslation * PositionTranslation

And the view matrix:

// View Matrix with Origin

-PositionTranslation * -OriginTranslation * -Rotation * Zoom * OriginTranslation

Example of transformations without origin, order: scale * rotation * translation

enter image description here