2015-01-16 34 views
0

我在UIImageView中有一個圖像。我已經成功地在圖像上畫出了線條。現在我想要刪除用戶將執行長按手勢的特定線條。我怎樣才能做到這一點 ?我的代碼是:在ios中從UIImage中刪除行

{

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



    } 
+0

PLZ添加您的代碼.... –

+0

@ Anbu.Karthik請看看我的代碼... –

回答

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); 
} 
+0

謝謝......我已經實施了方法,就像你建議的那樣。但有時它不會刪除被用戶擊中的行而是刪除其他的行。但大部分時間它的工作很好..... –

+0

嘗試分析問題的位置。如果沒有其他內容(-.7),您可能需要皮條客dotValue比較值。你也可能想要實現離線的最小距離。嘗試用這些值玩一下。 –

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

希望這對我有幫助。

+0

我要刪除哪一行用戶將執行長按手勢。例如,如果行從點A(234,445)開始到點B(400,600)。如果用戶在線執行手勢,那麼該線應該被刪除。用戶可以在任何一點執行長按手勢。 –

+0

在長按手勢方法中,您可以添加if和else條件,如this.if(觸摸位置> = 234 ||觸摸位置<= 600){//添加您的方法..} else {...}。 –