使用NSUserDefaults存储图像坐标

时间:2012-05-07 17:13:51

标签: cocoa nsuserdefaults

我正在我的应用程序中移动图像,这工作正常。问题是,当用户回到该视图时,我想保留图像。目前,我正在使用一个字符串来保存图像中心:

NSString *centre = NSStringFromCGPoint(image.center);

我有一个名为theCentre的NSUserDefault变量:

theCentre = [NSUserDefaults standardUserDefaults];

我尝试使用以下方式保存:

[theCentre setObject:centre forKey:@"centre"];

当我输出日志时,我得到以下内容:

{280, 320}

这很棒。 在viewDidLoad中,我尝试了完全相同的日志,它返回(null)。我认为这意味着它没有正确地保存中心。我错过了一些基本的东西,因为我在我的应用程序的其他地方使用过NSUserDefaults,它一直运行正常。非常感谢提前。
编辑:这是尝试和帮助调试的整个代码:)

-(void)getBounds
{
lowerBoundX = 250;
upperBoundX = 310;
lowerBoundY = 290;
upperBoundY = 350;

}

#pragma mark -
#pragma mark UIPanGestureRecognizer selector

- (void) dragGesture:(UIPanGestureRecognizer *) panGesture
{
 [self getBounds];

CGPoint translation = [panGesture translationInView:self.view];

switch (panGesture.state) {
    case UIGestureRecognizerStateBegan:{
        originalCentre = dragImage.center;
    }
        break;
    case UIGestureRecognizerStateChanged:{
        dragImage.center = CGPointMake(dragImage.center.x + translation.x,
                                       dragImage.center.y + translation.y);

    }
        break;
    case UIGestureRecognizerStateEnded:{            
        if (((dragImage.center.x >= lowerBoundX) && (dragImage.center.y >= lowerBoundY) &&
            (dragImage.center.x <= upperBoundX) && (dragImage.center.y <= upperBoundY))) {

            dragImage.center = CGPointMake(280, 320);
            [dragImage setUserInteractionEnabled:NO];

            theCentre = [NSUserDefaults standardUserDefaults];

        NSString *centre = NSStringFromCGPoint(dragImage.center);

            [theCentre setObject:centre forKey:@"centre"];

            NSLog(@"%@", [theCentre objectForKey:@"centre"]);
            [theCentre synchronize];

            break;
        }
        [UIView animateWithDuration:0.5 
                         animations:^{
                             dragImage.center = originalCentre;
                         }
                         completion:^(BOOL finished){
                         }];
    }
        break;
    default:
        break;
}

[panGesture setTranslation:CGPointZero inView:self.view];
}

-(IBAction)clear{
[UIImageView animateWithDuration:0.5 animations:^{
dragImage.center = CGPointMake(100, 200);
}];

[dragImage setUserInteractionEnabled:YES];
}

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

NSLog(@"%@", [theCentre objectForKey:@"centre"]);

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragGesture:)];

[dragImage addGestureRecognizer:panGesture];

[dragImage setUserInteractionEnabled:YES];

}

1 个答案:

答案 0 :(得分:2)

首先缓存NSUserDefaults中保存的数据,因此无法立即从NSUserDefaults的其他实例中获取。在某些预定义的事件/时间间隔内定期保存数据。但是,您可以通过调用synchronize来强制执行数据同步:

NSString *theCenter = NSStringFromCGPoint(image.center);
theCenter = [NSUserDefaults standardUserDefaults];
[theCenter setObject:centre forKey:@"center"];
[theCenter synchronize];