2013-02-12 92 views
1

我正在处理拖放应用程序,当用户拖放图像时,我想从拖动点中复制它,然后原始图像返回到初始点。 我决定执行touchesEnded后的UIImageView添加到我的ViewController,将子视图添加到选择器中的主要uiview中

我有一个包含该方法拖动视图类:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

CGPoint activePoint = [[touches anyObject] locationInView:self]; 
UIImageView *myimage; 
myimage.image = self.image; 
myimage.center = activePoint; 


ViewController *cview ; 
cview = [[ViewController alloc]init]; 
[cview getpoint: myimage]; 

} 

现在,在视图控制器,这是用GetPoint选择器:

-(void) getpoint : (UIImageView *) mine{ 
UIImageView *newimage; 
newimage = mine; 
[self.view addSubview:newimage]; 


NSLog(@" in getpoint"); 

} 

当我放弃物体时,出现此错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' 

但是当我删除addsubview语句时,NSlog正在向右

任何解决方案?

回答

0

你实际上并没有为你的UIImageViews分配内存。此代码是犯罪嫌疑人:

CGPoint activePoint = [[touches anyObject] locationInView:self]; 
UIImageView *myimage; 
myimage.image = self.image; 
myimage.center = activePoint; 

您需要分配/初始化您的图像视图

CGPoint activePoint = [[touches anyObject] locationInView:self]; 
UIImageView *myimage = [[UIImageView alloc] initWithFrame:rect]; 
myimage.image = self.image; 
myimage.center = activePoint; 

其中rect变量包含你想添加到层级图像视图的矩形尺寸。您不能将一个零对象添加到NSArray。因此,由于是UIImageView的零,此调用将会失败:

[self.view addSubview:newimage]; 
+0

我已经试过这可惜它没有工作 – Felwah 2013-02-16 11:44:55

+0

您是否提供了的CGRect具有适当大小到您的UIImageView initWithFrame:?你确定你传递的图像不是零吗?当你说“它没有用”时,你能详细说明你的意思吗? – 2013-02-16 16:23:02

相关问题