Ios 如何存储和检索UIImageView

Ios 如何存储和检索UIImageView,ios,objective-c,uiimageview,nsuserdefaults,Ios,Objective C,Uiimageview,Nsuserdefaults,我有两个UIImageViews,它们都包含uiRotationGestureRecognitizer,uiPinchGestureRecognitizer,uiPangestureRecognitizer。因此,我可以放大/缩小,更改UIImageView位置,还可以更改旋转。我想存储UIImageViewNSUserDefault,下次用户打开应用程序时,它会保持上次的状态,用户也可以撤消他们所做的更改 我的代码是: -(IBAction)Undo_BtnClick:(id)sender {

我有两个
UIImageView
s,它们都包含
uiRotationGestureRecognitizer
uiPinchGestureRecognitizer
uiPangestureRecognitizer
。因此,我可以放大/缩小,更改
UIImageView
位置,还可以更改旋转。我想存储
UIImageView
NSUserDefault
,下次用户打开应用程序时,它会保持上次的状态,用户也可以撤消他们所做的更改

我的代码是:

-(IBAction)Undo_BtnClick:(id)sender
{
    _UndoKey--;
    NSUserDefaults *savedata = [NSUserDefaults standardUserDefaults];
    self.imageView2=[savedata objectForKey:[NSString stringWithFormat:@"self.imageView1%i",_UndoKey]];
    self.imageView=[savedata objectForKey:[NSString stringWithFormat:@"self.imageView%i",_UndoKey]];



}
- (void)viewDidLoad
{
    [super viewDidLoad];
    _UndoKey = 0;

    CGFloat pointY = self.view.center.y;


    self.imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"png-0838"]];
    self.imageView2.frame  = CGRectMake(0, 0, 220, 220);
    self.imageView2.center = CGPointMake(self.view.center.x, pointY*2/3);
    self.imageView2.backgroundColor = [UIColor redColor];

    [self.view addSubview:self.imageView2];
    self.imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"music"]];
    self.imageView.frame  = CGRectMake(0, 0, 220, 220);
    self.imageView.center = CGPointMake(self.view.center.x, pointY*4/3);
    self.imageView.backgroundColor = [UIColor greenColor];

    [self.view addSubview:self.imageView];
    self.view.multipleTouchEnabled = YES;

    self.Undo_Btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    self.Undo_Btn.tag = 1;
    [self.Undo_Btn addTarget:self action:@selector(Undo_BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.Undo_Btn setTitle:@"Undo" forState:UIControlStateNormal];

    [self.Undo_Btn setBackgroundColor:[UIColor blackColor]];

    [self.view addSubview:self.Undo_Btn];


    UIRotationGestureRecognizer *rotationG = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationImage:)];
    rotationG.delegate = self;
    UIPinchGestureRecognizer *pinchGestureRecongnizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(changeImage:)];
    pinchGestureRecongnizer.delegate = self;
    [self.imageView setUserInteractionEnabled:YES];
    [self.imageView addGestureRecognizer:pinchGestureRecongnizer];
    [self.imageView addGestureRecognizer:rotationG];

    UIRotationGestureRecognizer *rotationG2 = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationImage2:)];
    rotationG2.delegate = self;
    UIPinchGestureRecognizer *pinchGestureRecongnizer2 = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(changeImage2:)];
    pinchGestureRecongnizer2.delegate = self;
        [self.imageView2 setUserInteractionEnabled:YES];
        [self.imageView2 addGestureRecognizer:pinchGestureRecongnizer2];
        [self.imageView2 addGestureRecognizer:rotationG2];

    UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panpan:)];
    [self.imageView addGestureRecognizer:panGesture];

    UIPanGestureRecognizer *panGesture2=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panpan2:)];
    [self.imageView2 addGestureRecognizer:panGesture2];
    self.frame1 = self.imageView.frame;
    self.frame2 = self.imageView2.frame;
}



- (void)panpan:(UIPanGestureRecognizer *)sender {
    [self.view bringSubviewToFront:self.imageView];
    CGPoint location = [sender locationInView:self.view];
    sender.view.center = CGPointMake(location.x,  location.y);
    if(sender.state == UIGestureRecognizerStateEnded)
    {
        [self SaveData];
    }
}
-(void)SaveData
{
    NSLog(@"::: %i :::",_UndoKey);
    _UndoKey++;

    NSUserDefaults *savedata = [NSUserDefaults standardUserDefaults];
    [savedata setObject:self.imageView forKey:[NSString stringWithFormat:@"self.imageView%i",_UndoKey]];
    [savedata setObject:self.imageView2 forKey:[NSString stringWithFormat:@"self.imageView2%i",_UndoKey]];
    [savedata synchronize];
}
- (void)panpan2:(UIPanGestureRecognizer *)sender {

    [self.view bringSubviewToFront:self.imageView2];
    CGPoint location = [sender locationInView:self.view];
    sender.view.center = CGPointMake(location.x,  location.y);

    if(sender.state == UIGestureRecognizerStateEnded)
    {
        [self SaveData];
    }
}


- (void)rotationImage:(UIRotationGestureRecognizer*)gesture {
    [self.view bringSubviewToFront:gesture.view];
    CGPoint location = [gesture locationInView:self.view];
    gesture.view.center = CGPointMake(location.x, location.y);
    if ([gesture state] == UIGestureRecognizerStateEnded) {
        self.lastRotation = 0;
        return;
    }
    CGAffineTransform currentTransform = self.imageView.transform;
    CGFloat rotation = 0.0 - (self.lastRotation - gesture.rotation);
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, rotation);
    self.imageView.transform = newTransform;
    self.lastRotation = gesture.rotation;
    if (gesture.state == UIGestureRecognizerStateEnded) {
        [self SaveData];
    }
}
- (void)changeImage:(UIPinchGestureRecognizer*)pinchGestureRecognizer {
    [self.view bringSubviewToFront:pinchGestureRecognizer.view];

    CGPoint location = [pinchGestureRecognizer locationInView:self.view];
    pinchGestureRecognizer.view.center = CGPointMake(location.x, location.y);
    pinchGestureRecognizer.view.transform = CGAffineTransformScale(pinchGestureRecognizer.view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
    pinchGestureRecognizer.scale = 1;
    if (pinchGestureRecognizer.state == UIGestureRecognizerStateEnded) {
        [self SaveData];
    }
}
- (void)changeImage2:(UIPinchGestureRecognizer*)pinchGestureRecognizer {
    [self.view bringSubviewToFront:pinchGestureRecognizer.view];
    CGPoint location = [pinchGestureRecognizer locationInView:self.view];
    pinchGestureRecognizer.view.center = CGPointMake(location.x, location.y);
    pinchGestureRecognizer.view.transform = CGAffineTransformScale(pinchGestureRecognizer.view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
    pinchGestureRecognizer.scale = 1;
    if (pinchGestureRecognizer.state == UIGestureRecognizerStateEnded) {
        [self SaveData];
    }
}
- (void)rotationImage2:(UIRotationGestureRecognizer*)gesture {

    [self.view bringSubviewToFront:gesture.view];
    CGPoint location = [gesture locationInView:self.view];
    gesture.view.center = CGPointMake(location.x, location.y);
    if ([gesture state] == UIGestureRecognizerStateEnded) {
        self.lastRotation = 0;
        return;
    }
    CGAffineTransform currentTransform = self.imageView2.transform;
    CGFloat rotation = 0.0 - (self.lastRotation - gesture.rotation);
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, rotation);
    self.imageView2.transform = newTransform;
    self.lastRotation = gesture.rotation;
    if (gesture.state == UIGestureRecognizerStateEnded) {
        [self SaveData];
    }
}
更改图像视图时出现此错误:

*** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '<UIImageView: 0x8164e00; frame = (50 223.333; 220 220); opaque = NO; gestureRecognizers = <NSArray: 0x81674d0>; layer = <CALayer: 0x8165d10>>' of class 'UIImageView'.  Note that dictionaries and arrays in property lists must also contain only property values.
2014-09-08 16:01:45.905 RotationGestureRecognizer[2695:c07] *** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '<UIImageView: 0x81634d0; frame = (165 7; 220 220); opaque = NO; gestureRecognizers = <NSArray: 0x8167750>; layer = <CALayer: 0x8163d00>>' of class 'UIImageView'.  Note that dictionaries and arrays in property lists must also contain only property values.
***-[NSUserDefaults setObject:forKey::尝试插入类“UIImageView”的非属性值“”。请注意,属性列表中的字典和数组也必须仅包含属性值。
2014-09-08 16:01:45.905 RotationGestureRecognitor[2695:c07]***-[NSUserDefaults setObject:forKey:]:尝试插入类“UIImageView”的非属性值“”。请注意,属性列表中的字典和数组也必须仅包含属性值。

您可以将其转换为NSData并存储图像

NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(self.imageview2.image);

NSUserDefaults *savedata = [NSUserDefaults standardUserDefaults];
    [savedata setObject:data forKey:[NSString stringWithFormat:@"self.imageView%i",_UndoKey]];
检索如下

NSData* imageData = [savedata objectForKey:[NSString stringWithFormat:@"self.imageView%i",_UndoKey]];
UIImage *image = [UIImage imageWithData:imageData];
希望它能帮助你

你可以这样做

-(void)SaveData
{
    NSLog(@"::: %i :::",_UndoKey);
    _UndoKey++;

    NSUserDefaults *savedata = [NSUserDefaults standardUserDefaults];


    [savedata setObject:UIImageJPEGRepresentation(self.imageView.image, 1.0)
                 forKey:[NSString stringWithFormat:@"self.imageView%i",_UndoKey]];
    [savedata setObject:UIImageJPEGRepresentation(self.imageView2.image, 1.0)
                 forKey:[NSString stringWithFormat:@"self.imageView2%i",_UndoKey]];
    [savedata synchronize];
}

-(IBAction)Undo_BtnClick:(id)sender
{
    _UndoKey--;
    NSUserDefaults *savedata = [NSUserDefaults standardUserDefaults];
    self.imageView2.image = [UIImage imageWithData:[savedata objectForKey:[NSString stringWithFormat:@"self.imageView1%i",_UndoKey]]];
    self.imageView.image = [UIImage imageWithData:[savedata objectForKey:[NSString stringWithFormat:@"self.imageView%i",_UndoKey]]];
}
多亏了“rptwsthi”,我找到了一个解决方案

用于在NSUserDefaults中保存uiimageview设置

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:NSStringFromCGRect(self.imageView.bounds) forKey:[NSString stringWithFormat:@"rect%i",_UndoKey]];
    [userDefaults setObject:NSStringFromCGRect(self.imageView2.bounds) forKey:[NSString stringWithFormat:@"rect2%i",_UndoKey]];
    [userDefaults setObject:NSStringFromCGAffineTransform(self.imageView.transform) forKey:[NSString stringWithFormat:@"transform%i",_UndoKey]];
    [userDefaults setObject:NSStringFromCGAffineTransform(self.imageView2.transform) forKey:[NSString stringWithFormat:@"transform2%i",_UndoKey]];
    [userDefaults setObject:NSStringFromCGPoint(self.imageView.center) forKey:[NSString stringWithFormat:@"point%i",_UndoKey]];
    [userDefaults setObject:NSStringFromCGPoint(self.imageView2.center) forKey:[NSString stringWithFormat:@"point2%i",_UndoKey]];
    [userDefaults synchronize];
从NSUserDefaults返回并设置

 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [self.imageView setBounds:CGRectFromString([userDefaults valueForKey:[NSString stringWithFormat:@"rect%i",_UndoKey]])];
        [self.imageView2 setBounds:CGRectFromString([userDefaults valueForKey:[NSString stringWithFormat:@"rect2%i",_UndoKey]])];
        [self.imageView setTransform:CGAffineTransformFromString([userDefaults valueForKey:[NSString stringWithFormat:@"transform%i",_UndoKey]])];
        [self.imageView2 setTransform:CGAffineTransformFromString([userDefaults valueForKey:[NSString stringWithFormat:@"transform2%i",_UndoKey]])];
        [self.imageView setCenter:CGPointFromString([userDefaults valueForKey:[NSString stringWithFormat:@"point%i",_UndoKey]])];
        [self.imageView2 setCenter:CGPointFromString([userDefaults valueForKey:[NSString stringWithFormat:@"point2%i",_UndoKey]])];

无法将UIImage存储在
NSUserDefaults
中。您必须在其中存储UIimage的NSData。@YogeshSuthar好的,谢谢您的回复。。那么我该如何存储图像视图的放大/缩小、定位和旋转呢?与其存储图像视图,不如存储图像视图在特定时刻保持的设置。@rptwsthi谢谢您的回复。谢谢您的回复。我知道我不想要商店图片查看图片我想要商店图片查看谢谢回复。我知道我不想要存储imageview图像我想要存储imageview图像我不知道确切的情况,但可能您无法在用户默认设置下存储cUnitrol并检索回来,因为cUnitrol指向某个内存位置,当您尝试再次加载时,可能是该位置上有其他东西