如何检查UIView在目标c中是否为零

我是iOS的新手,我面临着对视图进行validation的问题。 我已经创建了像Signature一样的视图如何在UIView上绘制签名 ,现在我想通过使用此代码进行validation

-(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]; } } 

但我没有成功。 请告诉我我做错了什么。

在此处输入图像描述

我想做validation。

在此处输入图像描述

如果我没有签名则会显示该消息。

请给我一些建议。

提前致谢!

我保存图像和使用条件

 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]; } 

尝试这个代码,它的确有效。

为此我的建议是检查UIImage而不是UIView ,这意味着你是否获得了签名图像。 然后你应该检查一下

 if(singatureImage1 == nil){ } else{ } 

而另一个签名

 if(singatureImage2 == nil) { } else{ } 

如果你没有签名没有nil图像,那么使用SinatureView ,如果你没有签名就会给你零图像然后你可以validation它。

如果您检查UIView (即您的签名视图)是否为零,那么您将始终不是nil视图,因为您已初始化它。

请检查这个答案

  UIGraphicsBeginImageContext(self.drawSignView.bounds.size); [[self.drawSignView.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0); NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((viewImage), 0.5)]; int imageSize = imgData.length; NSLog(@"size of image in KB: %d ", imageSize/1024); UIGraphicsBeginImageContext(self.drawSignViewClient.bounds.size); [[self.drawSignViewClient.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImageClient = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0); NSData *imgDataClient = [[NSData alloc] initWithData:UIImageJPEGRepresentation((viewImageClient), 0.5)]; int imageSizeClient = imgDataClient.length; NSLog(@"size of image in KB: %d ", imageSizeClient/1024); int OCS=imageSize/1024; int Client=imageSizeClient/1024; if(OCS<3) { alertviewshow=[[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Please do Signature " delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alertviewshow setTag:1]; [alertviewshow show]; } else if (Client<3) { alertviewshow=[[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Please do Signature " delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alertviewshow setTag:1]; [alertviewshow show]; } else { //Write Your Code.... }