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 会看到一个“正常”的图像。

现在,这就是我绘制签名的方式:

声明 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

于 2012-08-13T07:16:15.257 回答