我是 iOS 新手,在验证 View 方面遇到问题。我创建了类似签名的 View How to draw Signature on UIView ,现在我想使用此代码进行验证
-(IBAction)SavebtnClick:(id)sender
{
if(drawSignView==nil)
{
UIAlertView *alertviewshow =[[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Pease Sign" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertviewshow show];
}
else if (drawSignViewClient==nil)
{
UIAlertView *alertviewshow =[[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Please Sign" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertviewshow show];
}
else
{
viewcontroller1 *management =[[viewcontroller1 alloc] initWithNibName:@"viewcontroller1" bundle:nil];
[self.navigationController pushViewController:management animated:YES];
}
}
但我没有取得成功。请告诉我我做错了什么。
我想做验证。
如果我没有签名,则会显示该消息。
请给我建议。
提前致谢!
我保存图像和使用条件
UIGraphicsBeginImageContext(self.drawSignView.bounds.size);
[[self.drawSignView.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0);
// Store the data
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *imageData = UIImageJPEGRepresentation(viewImage, 100);
[defaults setObject:imageData forKey:@"image"];
[defaults synchronize];
if(viewImage==nil)
{
UIAlertView *alertviewshow =[[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Pease Sign" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertviewshow setTag:1];
[alertviewshow show];
}
但它不起作用,因为它包含空白图像。
最佳答案
UIGraphicsBeginImageContextWithOptions(self.drawSignView.bounds.size, NO, [UIScreen mainScreen].scale);
[self.drawSignView drawViewHierarchyInRect:self.drawSignView.bounds afterScreenUpdates:YES];
UIImage *signImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(signImage)
{
//Get Image from sign view Succesfully
}
else
{
UIAlertView *alertviewshow =[[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Please Sign" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertviewshow show];
}
尝试一下这段代码,它绝对有效。
关于ios - 如何在 Objective C 中检查 UIView 是否为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41518435/