How to check UIView is nil or not in objective c - ios

I am new in iOS and I am facing problem regarding to do validation of view. I have created Signature like view
How to draw Signature on UIView, and now I want to do validation by using this code
-(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];
}
}
But I am not getting success. Please tell me what I am doing wrong.
I want to do validation.
if I have not sign it shows the message.
Please give me suggestion.
Thanks in advance!
I save Image and used condition
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];
}
But it not work because it contain blank image.

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];
}
Try this code its definatly work.

For that my suggestion is to check UIImage rather then UIView, That means are you getting the signature image or not. Then you should check like
if(singatureImage1 == nil){
}
else{
}
And for another signature
if(singatureImage2 == nil)
{
}
else{
}
And if you getting not nil image without sign it then use SinatureView which will give you nil image if you did't sign it then you can validate it.
If you check the UIView(i.e. Your signature view) is nil or not,You will get always not nil view because you have initialized it.

Please check this answer
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....
}

Related

Adding UIImage to UIAlertView is not working as expected

I am working on an app. I tried to add an UIIMage to UIAlertView using below code,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Instruction"
message:#"Please TAP on Screen to Continue."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 50, 32, 32)];
UIImage *img = [UIImage imageNamed:#"pendingImg.png"];
[imageView setImage:img];
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[alert setValue:imageView forKey:#"accessoryView"];
}else{
[alert addSubview:imageView];
}
[alert show];
My image is of dimension 32 × 32 pixels. I am getting the alert as shown,
Should I add constraints to this image? or anything else to be made?
You can set imageView contentMode to UIViewContentModeScaleAspectFit,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Instruction"
message:#"Please TAP on Screen to Continue."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 50, 32, 32)];
UIImage *img = [UIImage imageNamed:#"pendingImg.png"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imageView setImage:img];
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[alert setValue:imageView forKey:#"accessoryView"];
}else{
[alert addSubview:imageView];
}
[alert show];
Might be Anbu.Karthik suggested link comment also helpful for you.
try this
UIAlertView* alert = [UIAlertView alloc] initWithTitle: #"Instruction" message: #"Please TAP on Screen to Continue." delegate:nil cancelButtonTitle:#"no" otherButtonTitles:#"yes", nil];
UIImage* img = [UIImage imageNamed:#"pendingImg.png"];
UIImageView* imgview = [UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
[img setImage:imgMyImage];
[alert setValue: imgview forKey:#"accessoryView"];
[alert show];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle: #"Instruction" message: #"Please TAP on Screen to Continue." delegate:nil cancelButtonTitle:#"no" otherButtonTitles:#"yes", nil];
UIImage* img = [UIImage imageNamed:#"add.png"];
UIImageView* imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
[imgview setImage:img];
[alert setValue: imgview forKey:#"accessoryView"];
[alert show];
}

Send Image with text message

I want to send an image with my text Message. I have used MFMessageComposeViewController. This is the code i have done:
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
NSArray *recipents = #[[arr_promoterDetaildata valueForKey:#"phone_number"], #"72345524"];
UIImage *img = [UIImage imageNamed:#"ic_dummy_img"];
NSData *imgData = UIImagePNGRepresentation(img);
BOOL didAttachImage = [messageController addAttachmentData:imgData typeIdentifier:(NSString *)kUTTypePNG filename:#"image.png"];
NSString *message = [NSString stringWithFormat:#"Sending SMS"];
[messageController setRecipients:recipents];
[messageController setBody:message];
if (didAttachImage)
{
// Present message view controller on screen
[self presentViewController:messageController animated:YES completion:nil];
}
else
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Failed to attach image"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[warningAlert show];
return;
}
I have tried so many method’s but it is not attaching image in my message screen. Can anyone help?

How to take snapshot of app running on simulator?

I used this code to take the snapshot of my app running on simulator, the quality of the snap is not good, Any idea?
-(IBAction)btnSave:(id)sender
{
UIGraphicsBeginImageContextWithOptions(Self.View.bounds.size,Self.View.opaque,10.0f);
[Self.View.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
/* Render the screen shot at custom resolution */
CGRect cropRect = CGRectMake(0,0,320,500);
UIGraphicsBeginImageContextWithOptions(cropRect.size,BrochureSave.opaque,10.0f);
[screenshot drawInRect:cropRect];
UIImage * customScreenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
/* Save to the photo album */
// UIImageWriteToSavedPhotosAlbum(customScreenShot , nil, nil, nil);
[self.library saveImage:customScreenShot toAlbum:#"Cemara roll" withCompletionBlock:^(NSError *error) {
if (error!=nil)
{
NSLog(#"Big error: %#",[error description]);
}
else
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Message" message:#"Image Save Cemara roll" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}];
}
Try changing 10.0f to 0.0f in UIGraphicsBeginImageContextWithOptions:
here:
UIGraphicsBeginImageContextWithOptions(Self.View.bounds.size,Self.View.opaque,0.0f);

Why is my core data saving slow?

Every time I try to save my NSManagedObjectContex, it takes 1-8 seconds each time. Here is my code:
- (IBAction)save
{
if (self.managedObjectContext == nil) {
self.managedObjectContext = [(RootAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
if (self.brandText.text.length == 0 || self.modelText.text.length == 0) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Error" message:#"Please fill out the required fields" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(125, 40, 31, 7)];
NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"bullet.png"]];
UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path];
[imageView setImage:bkgImg];
[alertView addSubview:imageView];
[alertView show];
} else {
Hand *a = [NSEntityDescription insertNewObjectForEntityForName:#"Hand" inManagedObjectContext:self.managedObjectContext];
a.brand = self.brandText.text;
a.caliber = self.cText.text;
self.managedObjectContext = self.app.managedObjectContext;
a.notes = self.notesView.text;
a.serialNumber = self.serialNumberText.text;
a.nickname = self.nicknameText.text;
a.model = self.modelText.text;
a.gunImage = self.image.image;
a.roundsFired = [NSString stringWithFormat:#"0"];
a.cleanRounds = [NSString stringWithFormat:#"500"];
a.showBadge = [NSNumber numberWithBool:YES];
[self dismissViewControllerAnimated:YES completion:nil];
NSError *error;
if (![self.managedObjectContext save:&error]) {
UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:#"Error" message:#"There was an internal error. \n Please restart the app and try again, Thank You" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[errorAlert show];
}
}
}
The code just saves the all of the textFields text. What is the reason its so slow? Any help is greatly appreciated.
Based on your question is quite difficult to understand what is going on but I would modify the else statement as follows...
else {
Hand *a = [NSEntityDescription insertNewObjectForEntityForName:#"Hand" inManagedObjectContext:self.managedObjectContext];
a.brand = self.brandText.text;
a.caliber = self.cText.text;
// why this?
// self.managedObjectContext = self.app.managedObjectContext;
a.notes = self.notesView.text;
a.serialNumber = self.serialNumberText.text;
a.nickname = self.nicknameText.text;
a.model = self.modelText.text;
a.gunImage = self.image.image;
a.roundsFired = [NSString stringWithFormat:#"0"];
a.cleanRounds = [NSString stringWithFormat:#"500"];
a.showBadge = [NSNumber numberWithBool:YES];
NSError *error;
if (![self.managedObjectContext save:&error]) { // error during saving
UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:#"Error" message:#"There was an internal error. \n Please restart the app and try again, Thank You" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[errorAlert show];
} else { // the save completes correctly, dismiss the view controller
[self dismissViewControllerAnimated:YES completion:nil];
}
}
If you want to monitor Core Data you should do it through Instrument. In addition you could set up Xcode as follows: XCode4 and Core Data: How to enable SQL Debugging.
Edit
Since the slow saving is due (based on your comment) to images, you should relies on these rules.
Core Data - Storing Images (iPhone)
Edit 2
Ok, since you don't know in advance about the size (in bytes) of your image, I really suggest to store the image in the filesystem and not in the Core Data store. In the db save only the path to your image. The image will be saved in background. This to prevent the UI to block.
Otherwise, if iOS 5 is the minimum requirement, use the External Storage flag.
How to enable External Storage for Binary Data in Core Data
Hope that helps.

failed to share my photo to WECHAT

I want to share a photo from the Internet to WECHAT, but after I press the share button , nothing happens , I am new to Objective-C and I am unable to figure out the problem myself.Hearing that there are a lot of experts here,so is there some guy can help me solve this one? thanks in advance. here are the codes:
UIImage * image = [[[imageTitleArray objectAtIndex:initIndex] albumImageView] image];
WXMediaMessage *message = [WXMediaMessage message];
[message setThumbImage: image];
WXImageObject *ext = [WXImageObject object];
ext.imageData = UIImageJPEGRepresentation(image,1);
message.mediaObject = ext;
SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease];
req.bText = NO;
req.message = message;
[WXApi sendReq:req];
Try the following method
- (void) sendImageContentToWeixin:(UIImage *)image {
//if the Weixin app is not installed, show an error
if (![WXApi isWXAppInstalled]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"The Weixin app is not installed" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
return;
}
//create a message object
WXMediaMessage *message = [WXMediaMessage message];
//set the thumbnail image. This MUST be less than 32kb, or sendReq may return NO.
//we'll just use the full image resized to 100x100 pixels for now
[message setThumbImage:[image resizedImage:CGSizeMake(100,100) interpolationQuality:kCGInterpolationDefault]];
//create an image object and set the image data as a JPG representation of our UIImage
WXImageObject *ext = [WXImageObject object];
ext.imageData = UIImageJPEGRepresentation(image, 0.8);
message.mediaObject = ext;
//create a request
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
//this is a multimedia message, not a text message
req.bText = NO;
//set the message
req.message = message;
//set the "scene", WXSceneTimeline is for "moments". WXSceneSession allows the user to send a message to friends
req.scene = WXSceneTimeline;
//try to send the request
if (![WXApi sendReq:req]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Error" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
}

Resources