Saving and restoring rotation and size of UIImageView

I am developing an application that, among other things, needs to process representations of images, that is, rotate, resize, move them ... save this in the basic data model and restore them on demand.

Rotation and scaling are done using gesture recognizers. I read here that after the conversion, you cannot use the image frame to get a real rectangle, you should use the borders and center instead.

I tried several combinations of saving / restoring the presentation frame, viewing the layer frame, presentation boundaries, presentation level boundaries, with no luck. This is my best approach, but image recovery is larger (although only) if the rotation angle is not 0 (higher angle, larger size of the restored image).

I understand that storing frame values ​​and their use when restoring borders is not consistent, but I get "better" results with this.

This is the code used to save image settings:

- (void)updateModelCoords {

    CGFloat angle = atan2f(myImageView.transform.b, myImageView.transform.b);
    CGRect frame = [myImageView.layer frame];
    [myModel setW:[[NSNumber alloc] initWithFloat:frame.size.width]];
    [myModel setH:[[NSNumber alloc] initWithFloat:frame.size.height]];
    [myModel setX:[[NSNumber alloc] initWithFloat:frame.origin.x]];
    [myModel setY:[[NSNumber alloc] initWithFloat:frame.origin.y]];
    [myModel setCenterX:[[NSNumber alloc] initWithFloat:[myImageView center].x]];
    [myModel setCenterY:[[NSNumber alloc] initWithFloat:[myImageView center].y]];
    [myModel setAngle:[[NSNumber alloc] initWithFloat:angle]];

    // db save stuff...
}

This is the code used to restore image settings:

- (void) restoreImage {
    float angle = [[myModel angle] floatValue];
    CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
    myImageView.transform = transform;
    CGRect bounds = [myImageView.layer bounds];
    bounds.size.width = [[myModel w] floatValue];
    bounds.size.height = [[myModel h] floatValue];
    bounds.origin.x = [[myModel x] floatValue];
    bounds.origin.y = [[myModel y] floatValue];
    [myImageView.layer setBounds:bounds];
    CGPoint center = CGPointMake([[myModel centerX] floatValue], [[myModel centerY] floatValue]);
    [myImageView setCenter:center];
}

I am applying a blue border to the image viewer layer as an assistant. Orientation and position are the same, but the shape is a square after restoration, and a rectangle before saving.

Tips will be very helpful.

,

+5
1

, , , , . , BOUNDS BOUNDS. .

  • Transform

, :

-(void)writeImageState {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:NSStringFromCGAffineTransform(self.transform) forKey:@"transform"];
[dict setObject:NSStringFromCGPoint(self.center) forKey:@"center"];
[dict setObject:NSStringFromCGRect(self.bounds) forKey:@"bounds"];

[NSKeyedArchiver archiveRootObject:dict toFile:forUser.imageTransformationPath];
[dict release];
}
-(void)readImageState {
id obj = [NSKeyedUnarchiver unarchiveObjectWithFile:forUser.imageTransformationPath];
if(obj == nil) return;

CGAffineTransform transformed = CGAffineTransformFromString([obj objectForKey:@"transform"]);
CGPoint centered = CGPointFromString([obj objectForKey:@"center"]);
CGRect rect = CGRectFromString([obj objectForKey:@"bounds"]);

self.bounds = rect;
self.center = centered;
self.transform = transformed;
}

, .

+12

All Articles