2012-08-13 25 views
0

現在我將繪製的簽名保存在畫布上作爲圖像,但只要我在UIImageView中顯示圖像,也會顯示白色背景。我不想用白色背景顯示它,我只是想顯示簽名。我怎樣才能做到這一點?如何捕捉沒有背景的畫布繪製簽名?

下面是一段代碼保存:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyFolder"]; 
NSError *error = nil; 
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; 


CGSize size = self.view.bounds.size; 
CGRect cropRect = CGRectMake(10, 50, 640, 300); 

/* Get the entire on screen map as Image */ 
UIGraphicsBeginImageContext(size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage * Image1 = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 


/* Crop the desired region */ 
CGImageRef imageRef = CGImageCreateWithImageInRect(Image1.CGImage, cropRect); 
UIImage * cropImage = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef); 

UIGraphicsEndImageContext(); 

canvasImage.image = cropImage; 

//for saving image to documents directory 
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
NSString *documentsDirectory1 = [paths1 objectAtIndex:0]; 
NSString *savedImagePath1 = [documentsDirectory1 stringByAppendingPathComponent:@"MyFolder/signatureImage.png"]; 
UIImage *image = canvasImage.image; // imageView is my image from camera 
NSData *imageData = UIImagePNGRepresentation(cropImage); 
[imageData writeToFile:savedImagePath1 atomically:NO];  
+1

發佈您的嘗試,讓我們知道你的方式沒有。然後,我們會知道你在代碼中有一個小錯誤,或者通過採取錯誤的方法是一個巨大的錯誤。 – 2012-08-13 06:44:35

+0

@TeodorCarstea請用一段代碼檢查我更新的問題。 – sachi 2012-08-13 06:52:33

+0

問題:您正在繪製簽名(某些圖形,無論什麼),並且您需要它在透明背景上?我幫你解決了嗎? – 2012-08-13 06:55:28

回答

1
  1. 考慮您對您繪製的UIImageView。
  2. 打開你想要的圖像編輯器,並製作一個圖像:它必須是完全白色和99%透明的。

爲什麼:如果圖像上下文是100%透明的,圖像上下文會遇到麻煩。所以你做一個白色的圖像,讓我們說99.9%透明,所以用戶的眼睛會看到一個透明的圖像,但目標C會看到一個「正常」的。

現在,這是我畫的簽名方式:

聲明的ivars:

UIImageView image_signature; 
CGPoint lastP; 

然後:

//___________________________________________________ 
-(void)drawLineFromX:(float)x fromY:(float)y toX:(float)xx toY:(float)yy{ 
    UIGraphicsBeginImageContext(image_signature.image.size); 
    [image_signature.image drawInRect:CGRectMake(0, 0, image_signature.image.size.width, image_signature.image.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), x, y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xx, yy); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    [image_signature setImage:UIGraphicsGetImageFromCurrentImageContext()]; 
    UIGraphicsEndImageContext(); 
} 
//___________________________________________________ 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    lastP = [touch locationInView:self.view]; 
    [button_authorize setEnabled:YES]; 

} 
//___________________________________________________ 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint nextP = [touch locationInView:image_signature]; 
    [self drawLineFromX:lastP.x fromY:lastP.y toX:nextP.x toY:nextP.y]; 
    lastP = nextP; 
} 
//___________________________________________________ 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint nextP = [touch locationInView:image_signature]; 
    //and this will allow you to draw a point, you know, some people 
    //use dots in their signatures   
    [self drawLineFromX:lastP.x fromY:lastP.y toX:nextP.x toY:nextP.y]; 
    lastP = nextP; 
} 

,並清除簽名(你有德魯)只需再次將99%的白色透明圖像加載到「image_signature」imageView

+0

@Teosor Carstea這是用於在畫布上繪畫。我需要繪製後我需要捕獲繪製的簽名。 – sachi 2012-08-13 08:44:57

+0

夥計,你不明白嗎?你不需要捕獲任何東西。你所有的就是使用「image_signature」,因爲我的方法立即將簽名(你畫的所有東西)寫入它。 – 2012-08-13 08:50:13

+0

沒錯。但我的要求是,例如,如果我畫「S」,我需要將繪製的圖像保存爲「S」而沒有白色背景。你所做的只是畫在屏幕上。 – sachi 2012-08-14 05:49:50