转换为NSStringFromCGAffineTransform

时间:2011-11-22 16:14:30

标签: iphone transformation

我正在尝试保存已转换(旋转,缩放和移动)UIImageView并将转换加载回屏幕,但它根本不起作用。每次我尝试,UIImageView都应该在不同的地方。

这里有一些代码:

保存到UserDefaults

NSUserDefaults * UserDefs = [NSUserDefaults standardUserDefaults];
UIImageView * petit = (UIImageView*)[ConView viewWithTag:1];
[UserDefs setBool:YES forKey:@"gespeichert"];
[UserDefs setObject:NSStringFromCGRect([petit frame]) forKey:@"frame"];
[UserDefs setObject:NSStringFromCGAffineTransform(petit.transform) forKey:@"position1"];
[UserDefs synchronize];

从UserDefaults加载:

CGAffineTransform transformation = CGAffineTransformFromString([UserDefs objectForKey:@"position1"]);
    su.transform = transformation; // "su" is an UIImageView - allocated with Frame from:
    CGRect RectFrame = CGRectFromString([UserDefs objectForKey:@"frame"]);

我错了什么? 也许有人可以帮助我。

##############

编辑: 我现在改了代码: 用于加载

CGRect frameGroesse;
    frameGroesse = CGRectFromString([UserDefs objectForKey:@"frame"]);
    UIImageView * su = [[UIImageView alloc] initWithFrame:frameGroesse];
    su.image = [UIImage imageNamed:@"Xcode.jpg"];
    su.contentMode = UIViewContentModeScaleAspectFit;
    su.multipleTouchEnabled = YES;
    su.tag = 1;
    [su setUserInteractionEnabled:YES];
    [self addGestureRecognizersToPiece:su];
    CGAffineTransform transformation = CGAffineTransformFromString([UserDefs objectForKey:@"position1"]);
    su.transform = transformation;
    su.center = CGPointFromString([UserDefs objectForKey:@"center"]);

    [ConView addSubview:su];
    [ConView bringSubviewToFront:su];
    [ConView bringSubviewToFront:[ConView viewWithTag:1]];
    [su release];

保存:

NSUserDefaults * UserDefs = [NSUserDefaults standardUserDefaults];
UIImageView * petit = (UIImageView*)[ConView viewWithTag:1];
[UserDefs setBool:YES forKey:@"gespeichert"];
[UserDefs setObject:NSStringFromCGRect([petit bounds]) forKey:@"frame"];
[UserDefs setObject:NSStringFromCGPoint(petit.center) forKey:@"center"];

[UserDefs setObject:NSStringFromCGAffineTransform(petit.transform) forKey:@"position1"];
[UserDefs synchronize];

但是在加载图像后,我仍然会得到一个小动作。还是需要一些帮助。

2 个答案:

答案 0 :(得分:0)

CGAffineTransform可能会使您应用转换的视图的frame属性无效,因此您可能没有存储有用的帧...

答案 1 :(得分:0)

您不应将transformboundscenterframe混合使用。例如,将transform设置为视图会影响其框架。至于你的UIImageView:它在superview中的位置应由其center定义。它的旋转/比例应由transform定义(为简单起见,不要将转换放在转换中)。大小应由bounds.size定义。所以,你必须存储三个项目。