現在我將繪製的簽名保存在畫布上作爲圖像,但只要我在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];
發佈您的嘗試,讓我們知道你的方式沒有。然後,我們會知道你在代碼中有一個小錯誤,或者通過採取錯誤的方法是一個巨大的錯誤。 – 2012-08-13 06:44:35
@TeodorCarstea請用一段代碼檢查我更新的問題。 – sachi 2012-08-13 06:52:33
問題:您正在繪製簽名(某些圖形,無論什麼),並且您需要它在透明背景上?我幫你解決了嗎? – 2012-08-13 06:55:28