这是一个很难解释的问题...但我会尽力而为。 首先是问题的背景,基本上我正在为 ios 创建一个类似绘画的应用程序,并希望添加一个功能,允许用户选择图像的一部分(多点触控显示不透明的矩形)并删除/复制粘贴/旋转那部分。我已经使删除和复制粘贴工作完美,但旋转是另一回事。要旋转图像的一部分,我首先复制图像的一部分并将其设置为所选矩形图层的背景,然后用户使用 slider 旋转任意角度。问题是,有时图像最终会从矩形的另一个位置显示(这意味着复制的图像卡在矩形的错误角上)。我认为这可能是我的fragment.frame.origin的问题,但通过各种测试,该值似乎是正确的。它似乎也会根据阻力的方向而变化......
这些是问题的屏幕
在上述每种情况下,图像的不匹配部分应该在灰色矩形内,我不知道问题是什么。
bg = [[UIImageView alloc] initWithImage:[self crop:rectangle.frame:drawImage.image]];
[rectangle addSubview:bg];
drawImage是用户绘制的,矩形是选中的灰色区域。 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;
这与我绘制矩形的方式有什么关系吗? (上)我从 TouchMoved 方法的一部分调用此方法(下),这可能会导致问题(触摸 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];
}
最佳答案
我不确定这是否是您的问题,但看起来您只是假设 touch1 代表左上角触摸。我将从标准化矩形开始。
// Standardizing the rectangle before making it the frame.
CGRect frame = CGRectStandardize(CGRectMake(x1, y1, x2-x1, y2-y1));
rectangle = [[UIView alloc] initWithFrame:frame];
关于iphone - uiimage 未正确放入 subview 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11571100/