从 ios 中的 UIImage 中删除行 [英] Removing lines from UIImage in ios

查看:16
本文介绍了从 ios 中的 UIImage 中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIImageView 中有一个图像.我已经成功地在图像上绘制了线条.现在我想删除用户将执行长按手势的特定线条.我怎样才能做到这一点 ?我的代码是:

I have an image in UIImageView. I have drawn lines on the image sucdcessfully.Now I want to remove that specific line on which user will perform a long press gesture. how can I do that ? my code is :

{

        // gets the location in the form of (x,y) coordinates
        CGPoint  location=[sender locationInView:imageView];
        // convetrs the location relative to the image
        CGPoint contextLocation = CGPointMake(location.x*imageView.image.size.width/imageView.frame.size.width, location.y*imageView.image.size.height/imageView.frame.size.height);
        // converts the location point into NSValue
        NSValue *imagePoint=[NSValue valueWithCGPoint:contextLocation];
        // Adds the  image into NSMutable Array
        [imageLocations addObject:imagePoint];
        // color of line
        UIColor * linearcolor=[UIColor blueColor];


        UIGraphicsBeginImageContext(imageView.image.size);

        [imageView.image drawAtPoint:CGPointMake(0, 0)];


        CGContextRef context=UIGraphicsGetCurrentContext();

        // Brush widt
        CGContextSetLineWidth(context, 3.0);
        // Line Color
        CGContextSetStrokeColorWithColor(context, [linearcolor CGColor]);
        // Hard coded point for location 2
        CGPoint location2=CGPointMake(300, 400);


        CGContextMoveToPoint(context, contextLocation.x, contextLocation.y);

        CGContextAddLineToPoint(context, location2.x, location2.y);
        CGContextStrokePath(context);

        newImage=UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();
        imageView.image=newImage;



    }

推荐答案

您需要做的是找到被按下的线,并将其从您拥有的点数组中移除,然后用这些线重新绘制整个图像.

What you need to do is find the line pressed and remove it from the array of points you have and then redraw the whole image with the lines.

我想问题在于如何找到正确的行:

I guess the problem is in how to find the correct line:

假设您从手势中获得了一个位置 L.然后你需要的是遍历所有的点对,比如

Lets say you have a location L gotten from the gesture. Then what you need is to iterate through all the point pairs such as

for(NSInteger i=0; i< imageLocations.count.count-1; i+=2)
{
   CGPoint T1 = [imageLocations[i] CGPointValue];
   CGPoint T2 = [imageLocations[i+1] CGPointValue];
}

现在你有LT1T2点.从这里有很多方法可以检测线路是否被击中.我最喜欢的是先用点积来看看印刷机是否像这样:

Now you have points L, T1, T2. From here there are many ways to detect if the line is hit. What I like most is to first use a dot product to see if the press is near the line like so:

CGFloat dotValue = dot(normalized(T1-L), normalized(T2-L))

在线查找点积和点归一化的过程.现在 dotValue 应该接近 -1 才能命中该行.您应该使用诸如 if(dotValue > -.7f) continue; 之类的东西来删除沿线距离印刷机太远的所有线.

Find the procedures for the dot product and the normalization of a point online. Now the dotValue should be near -1 for the line to be hit. You should use something like if(dotValue > -.7f) continue; to remove all the lines too far from the press along the line.

接下来是实际找到与该点的直线距离.这实际上是叉积的 Z 分量:

Next is to actually find the line distance from the point. This is actually a Z component of a cross product:

CGFloat crossValue = fabsf(cross((L-T1), normalized(T2-T1)).z);

我知道你没有 Z 组件,所以使用:

I know you have no Z component so use:

crossValue = A.x*B.y - A.y*B.x

既然你已经有了这一切,伪代码应该是这样的:

So now that you have all this the pseudo code should be something like this:

CGPoint L;
NSInteger bestLineIndex = -1;
CGFloat bestLineDistance = 10000.0f; // something large

for (CGPoint T1, T2; NSInteger index)
{
    dotValue = ...
    if(dotValue < -.7)
    {
        crossValue = ...
        if(crossValue < bestLineDistance)
        {
           bestLineDistance = crossValue;
           bestLineIndex = index;
        }
    }
}

所以最后你有你的第一条线点的索引,如果不是 -1(如果 -1 在触摸附近没有找到线),应该删除它.

So in the end you have the index of your first line point which should be removed if not -1 (if -1 no line was found near the touch).

对于使用叉积的点的线距离:

For line distance from a point using a cross product:

- (CGFloat)distanceBetweenPoint:(CGPoint)L andLineWithStart:(CGPoint)T1 end:(CGPoint)T2
{
    CGPoint T1toL = CGPointMake(L.x-T1.x, L.y-T1.y);
    CGPoint T2toL = CGPointMake(L.x-T2.x, L.y-T2.y);

    CGFloat T2toLDistance = sqrt(T2toL.x*T2toL.x + T2toL.y*T2toL.y);
    CGPoint T2toLNormalized = CGPointMake(T2toL.x/T2toLDistance, T2toL.y/T2toLDistance);

    CGFloat zComponentOfCross = T1toL.x*T2toLNormalized.y - T1toL.y*T2toLNormalized.x;

    return fabs(zComponentOfCross);
}

这篇关于从 ios 中的 UIImage 中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆