我有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。 我的代码上有什么错误的想法吗?
答案 0 :(得分:0)
我正在尝试你的上述想法。无法获得输出。这是实现同样目标的另一个想法。 我正在使用图像在同一图像视图上显示图像变化"撤消"操作。我已经实现了如下方法。
它在我身边工作得很好。 我希望这会对我们有所帮助。
//撤消按钮操作
-(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];
}
}
//从公共变量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;
}