【问题标题】:uiimage not being put into subview correctlyuiimage 未正确放入子视图
【发布时间】:2012-07-19 05:50:18
【问题描述】:

这是一个很难解释的问题……但我会尽力而为。 首先是问题的背景,基本上我正在为 ios 创建一个类似绘画的应用程序,并希望添加一个功能,允许用户选择图像的一部分(多点触控显示不透明的矩形)和删除/复制粘贴/旋转那部分。我已经让删除和复制粘贴工作完美,但轮换是另一回事。为了旋转图像的一部分,我首先复制图像的一部分并将其设置为所选矩形图层的背景,然后用户使用滑块旋转任意角度。问题是有时图像最终会从矩形的另一个位置显示(这意味着复制的图像挂在矩形的错误角上)。我认为这可能是我的 rectangle.frame.origin 的问题,但通过各种测试,该值似乎是正确的。它似乎也根据拖动的方向而改变......

这些是问题的屏幕

在上述每种情况下,图像的不匹配部分都应该在灰色矩形内,我不知道问题是什么。

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

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

我在粘贴任意旋转的图像时也遇到了麻烦。关于如何做到这一点的任何想法?

编辑:添加更多代码。

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

【问题讨论】:

  • 我认为您需要在此处添加更多代码以帮助人们理解您的问题!
  • 我同意,只是需要考虑添加哪个部分。我认为我添加的部分可能是问题的根源
  • 说到添加代码,把上面所有的图片都放上去是一个很好的开始!这意味着您会花一点(大量)时间向试图提供帮助的人提供信息 - 非常好!

标签: iphone ios ipad uiview uiimageview


【解决方案1】:

我不确定这是否是您的问题,但您似乎只是假设 touch1 代表左上触摸。我将从标准化矩形开始。

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

【讨论】:

  • 谢谢,我认为这可能是问题,但不知道这是 1 行修复
猜你喜欢
  • 2023-03-06
  • 2010-12-30
  • 1970-01-01
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
  • 2019-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多