在ios中删除UIImage中的行

时间:2015-01-16 12:08:59

标签: ios uiimageview uiimage

我在UIImageView中有一个图像。我已经在图像上画了几行。现在我想删除用户将执行长按手势的特定行。我怎样才能做到这一点 ?我的代码是:

{

        // 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;



    }

2 个答案:

答案 0 :(得分:2)

很简单。我在下面添加我的代码作为您的问题。

// 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);

    **CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);**

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

希望这很有帮助。享受。

答案 1 :(得分:0)

您需要做的是找到所按下的线并将其从您拥有的点阵列中移除,然后用线条重新绘制整个图像。

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

假设您有一个位置L从手势中获得。那么你需要的是迭代所有的点对,如

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

现在您有积分LT1T2。从这里可以通过多种方式检测线路是否被击中。我最喜欢的是首先使用点积来查看印刷机是否接近这条线:

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

找到点积的程序和在线点的标准化。现在,dotValue应该接近-1,以便命中该行。你应该使用类似if(dotValue > -.7f) continue;之类的东西来删除距离印刷机太远的所有线条。

接下来是实际找到距离点的线距。这实际上是交叉产品的Z组件:

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

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

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

所以现在你拥有了所有这些伪代码应该是这样的:

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在触摸附近没有找到线)。

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

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