现在我将画布上绘制的签名保存为图像,但是一旦我在 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];