iOS如何进行撤消操作,以旋转,缩放和添加UiimageView



我有一个UIImageView,我可以在此UIImageView中添加新项目(作为子视图),也可以移动&旋转UIImageView中的项目。我尝试为此项目做出撤消操作,所以我的代码;

在每个新操作之前,我都会获得UIImageView的副本,然后将它们存储在NSMutableArray

[self.lastActions addObject:[self getCopy]];

我获得具有不同内存地址的相同的UIImageView。(我不确定这是正确的方法)

-(UIImageView *)getCopy{
    NSData *archivedViewData = [NSKeyedArchiver archivedDataWithRootObject:self.imageView];
    id clone = [NSKeyedUnarchiver unarchiveObjectWithData:archivedViewData];
    return clone;
}

当我撤消;

-(IBAction)undoButtonClicked:(id)sender{
    UIImageView *lastImageView = [self.lastActions lastObject];
    [UIView animateWithDuration:0.1 animations:^{
        self.imageView = lastImageView;
    }];
    [self.lastActions removeObject:lastImageView];     
}

,但它不会更改屏幕上的UIImageView。关于我的代码有什么问题的想法?

我正在尝试您的上述想法。无法获得输出。这是实现同样的另一个想法。我正在使用图像来揭示同一映像"撤消"操作上的图像更改。我已经像下面的方法一样实现。

它在我这边工作正常。我希望这对我们有帮助。

//撤消按钮动作

-(IBAction)undoButtonClicked:(id)sender{
      UIImage *lastImageView =  lastImageView = (UIImage *)[lastActions lastObject];
     if( [lastActions count]!=0) {
           [UIView animateWithDuration:0.2 animations:^{
           self.imageView.image = lastImageView;
           }];
           [lastActions removeObject:lastImageView];
      }
}

//从public varible self.imageview

复制图像
-(IBAction)copyButton:(id)sender{
    [lastActions addObject:self.imageView.image];
}    

//随机方向更改以旋转图像。

-(UIImageOrientation )randonImageOrientation {
   int min = 0 ;
   int max = 3;
   UIImageOrientation ori;
   int randomNumber = min + rand() % (max-min);
   switch (randomNumber)
   {
    case 0 :
        ori = UIImageOrientationDown;
        break;
    case 1:
        ori = UIImageOrientationLeft;
        break;
    case 2:
        ori = UIImageOrientationRight;
        break;
    case 3:
        ori = UIImageOrientationUp;
        break;
    default :
        ori = UIImageOrientationUp;
        break;
   }
return  ori;
}

//下面的方法使用旋转图像

 -(UIImage *)imageOrientation :(UIImageOrientation)imageOri image:(UIImage *)oriImage {
   UIImage * LandscapeImage = oriImage;
   UIImage * PortraitImage = [[UIImage alloc] initWithCGImage: LandscapeImage.CGImage
                                                     scale: 0.5
                                               orientation: imageOri];
   return PortraitImage;
 }

最新更新