gpt4 book ai didi

iphone - uiimage 未正确放入 subview

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:55 26 4
gpt4 key购买 nike

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

这些是问题的屏幕

1

2

3

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

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

最佳答案

我不确定这是否是你的问题,但看起来你只是假设 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com