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];      
4

1 に答える 1

1
  1. 描画するUIImageViewがあると考えてください。
  2. 目的の画像エディターを開き、画像を作成します。画像は完全に白く、99% 透明でなければなりません。

理由: この画像を UIImageView に渡します。画像が 100% 透明であると、グラフィック コンテキストを操作する際に問題が発生します。

つまり、白い画像、たとえば 99.9% の透明度を作成すると、ユーザーの目には透明な画像が表示されますが、Objective C には「通常の」画像が表示されます。

さて、これは私が署名を描く方法です:

ivar を宣言します。

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 に再度読み込みます。

于 2012-08-13T07:16:15.257 に答える