如何存储和检索UIImageView

时间:2014-09-08 10:32:52

标签: ios objective-c uiimageview nsuserdefaults

我有两个UIImageView,它们都包含UIRotationGestureRecognizerUIPinchGestureRecognizerUIPanGestureRecognizer。所以我可以放大/缩小,更改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.

3 个答案:

答案 0 :(得分:0)

您可以将其转换为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];

希望它可以帮助你......!

答案 1 :(得分:0)

你可以这样做

-(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]]];
}

答案 2 :(得分:0)

感谢“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和Set

返回
 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]])];