现在我将绘制的签名保存在画布上作为图像,但只要我在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];
答案 0 :(得分:1)
为什么:您将此图像提供给UIImageView,如果它是100%透明的,图形上下文将无法使用它。
所以你制作一个白色的图像,让我们说99.9%是透明的,所以用户的眼睛会看到一个透明的图像,但是Objective C会看到一个" normal"之一。
现在,这是我绘制签名的方式:
宣布伊娃:
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的