2017-01-07 36 views
1

我是iOS的新手,我面臨着驗證視圖的問題。我創建簽名狀觀 How to draw Signature on UIView,現在我想通過使用此代碼如何檢查UIView是否爲零或不在客觀c

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

做驗證,但我沒有得到成功。請告訴我我做錯了什麼。

enter image description here

我想要做驗證。

enter image description here

如果我沒有簽字顯示消息。

請給我建議。

在此先感謝!

我保存的圖像和使用條件

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

但它不工作,因爲它包含空白圖像。

+1

你能告訴更多的代碼是什麼?這段代碼的問題?你想要做什麼,詳細註明。 –

+0

請爲此解釋更多... –

+0

這是Signatureview的一個實例嗎? – Axel

回答

0

請檢查這個答案

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.... 
} 
1

對於我的建議是檢查UIImage,而不是UIView,這意味着你是否得到簽名圖像或不。然後,你應該檢查像

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

而對於另一個簽名

if(singatureImage2 == nil) 
{ 

} 
else{ 
} 

如果你沒有跡象越來越不無圖像它然後使用SinatureView,這將給你無圖像如果我以前不簽字,然後你可以驗證它。

如果您檢查UIView(即您的簽名視圖)是否爲零,您將永遠不會無視圖,因爲您已初始化它。

+0

@piysuhPatel是的,我正在獲得簽名圖像。 – Muju

+0

沒有簽名,你得到簽名圖像? –

+0

是的沒有簽名我也獲得白色圖像。 – Muju

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

試試這個代碼它的definatly工作。

+0

[self.drawSignView drawViewHierarchyInRect:self.drawSignView.bounds afterScreenUpdates:YES]; –

+0

不起作用。它總是進入如果條件。 – Muju