iphone - uiimage 未正确放入 subview 中

标签 iphone ios ipad uiview uiimageview

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

这些是问题的屏幕

1

2

3

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

     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/

相关文章:

ios - 为应用程序添加试用期。我应该在 Swift (iOS) 中使用 Timer 还是 DispatchQueue?

ios - 如何在 UITextFields 中设置验证

iphone - 错误 CGImageCreate : invalid image size: 0 x 0 with a (picker) window rotation

ios - Core Data "The Database appears corrupt"——是什么导致了这个错误?

iphone - 您可以使用 list 或网络存储在 iOS 网络应用程序中缓存声音文件吗?

iphone - C/生成一个 1 到 4 之间的随机数,用 arc4random( ) 去掉 3?

ios - 为什么一个方法工作得很好,但如果我把它放在 dispatch_async 中就会崩溃?

ios - Firebase 远程配置错误 8003,Unity,iOS

iphone - 访问文件夹iphone应用程序中的所有图像

iphone - 将 iPhone 应用程序更改为 iPad 应用程序的最简单方法是什么?