2012-07-20 51 views
2

这是一个难以解释的问题...但我会尽我所能。 首先是关于这个问题的背景,基本上我为ios创建了一个像app一样的绘画,并且希望添加一个允许用户选择图像的一部分的功能(多点触摸显示一个不透明的矩形)并删除/复制粘贴/旋转该部分。我已经完成了删除和复制粘贴工作,但旋转是另一回事。要旋转图像的一部分,我首先复制图像的一部分并将其设置为所选矩形图层的背景,然后用户使用滑块旋转任意角度。问题是,有时图像最终从矩形的另一个位置显示(意味着复制的图像悬挂在矩形的错误角落)。我认为这可能是我的rectangle.frame.origin问题,但通过各种测试,该值似乎是正确的。它似乎也取决于拖进去的方向变化......uiimage没有正确放入子视图

这些都是问题

1 http://i49.tinypic.com/dfu77m.jpg

2 http://i48.tinypic.com/14ikdxl.jpg

3 http://i46.tinypic.com/16kqpoi.jpg

的屏幕在每在上述情况下,图像的不匹配部分应该在灰色矩形内,我对于问题是什么而感到不知所措。

 bg = [[UIImageView alloc] initWithImage:[self crop:rectangle.frame:drawImage.image]]; 
    [rectangle addSubview:bg]; 

drawImage是用户绘图,矩形是选定的灰色区域。 crop是一种从给定rect返回给定图像的一部分的方法。

我也有麻烦粘贴任意旋转的图像..任何想法如何做到这一点?

编辑:添加更多代码。

-(void)drawRect:(int)x1:(int)y1:(int)x2:(int)y2{ 
    [rectangle removeFromSuperview]; 
    rectangle = [[UIView alloc] initWithFrame:CGRectMake(x1, y1, x2-x1, y2-y1)]; 
    rectangle.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.6]; 
    selectionImage = drawImage.image; 
    drawImage.image = selectionImage; 
    [drawImage addSubview:rectangle]; 
    rectangleVisible = true; 
    rectangle.transform = transformation; 

难道它与我如何画矩形有关吗? (上面)我从touchesMoved方法(下面)的一部分调用这个方法可能会导致问题(触摸1在错误的位置可能会导致宽度为负面?)如果是这样,是否有一个简单的方法来解决这个问题?

 if([[event allTouches] count] == 2 && !drawImage.hidden){ 
     NSSet *allTouches = [event allTouches]; 
     UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0]; 
     UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1]; 
     [self drawRect:[touch1 locationInView:drawImage].x :[touch1 locationInView:drawImage].y: 
     [touch2 locationInView:drawImage].x :[touch2 locationInView:drawImage].y]; 
    } 
+0

我想你需要在这里添加更多的代码来帮助人们理解你的问题! – trumpetlicks 2012-07-20 00:14:05

+0

我同意,只需要考虑添加哪个部分。我认为我添加的部分可能是问题的根源 – 2012-07-20 00:34:52

+0

关于添加代码,将所有图片放在上面是一个很好的开始!这意味着你花了一点时间向想要帮助的人提供信息 - 非常好! – 2012-07-20 00:36:43

回答

2

我不确定这是否是你的问题,但它看起来像你只是假设touch1代表左上角的触摸。我会通过标准化矩形开始。

// Standardizing the rectangle before making it the frame. 
CGRect frame = CGRectStandardize(CGRectMake(x1, y1, x2-x1, y2-y1)); 
rectangle = [[UIView alloc] initWithFrame:frame]; 
+0

谢谢,我认为这可能是问题,但不知道它是1行修复 – 2012-07-20 03:39:44