01-31-2012, 10:23 PM
#1 (permalink )
Registered Member
Join Date: Jun 2010
Posts: 210
Help with Showing and Hiding a UIImage
I'm working on adding a QR code in an app.
I'd like to have it set to only show the image when something has been placed in a UITextField
I'm trying something like this:
Code:
if ( (string = [NSString stringWithFormat:@"%@", aTextField.text]) )
{
UIImage * image = [QREncoder encode:string];
aImageView = [[UIImageView alloc] initWithImage:image];
.... more code
[aViewController.view addSubview:aImageView];
[aImageView release];
}
else
{
[self.aImageView removeFromSuperview];
}
The image is always there though
Any ideas to what I'm doing wrong?
01-31-2012, 10:41 PM
#2 (permalink )
Registered Member
Join Date: May 2009
Location: Stanford, CA
Posts: 291
I believe this:
Quote:
if ( (string = [NSString stringWithFormat:@"%@", aTextField.text]) )
Will always evaluate to true because the stringWithFormat will return an "empty string" even if there is no text.
Perhaps try something more like this:
Code:
string = [NSString stringWithFormat:@"%@", aTextField.text]
if ([string length] != 0) {
}
01-31-2012, 11:23 PM
#3 (permalink )
Registered Member
Join Date: Jun 2010
Posts: 210
makes much more sense, but the image is still always showing
01-31-2012, 11:26 PM
#4 (permalink )
Registered Member
Join Date: May 2009
Location: Stanford, CA
Posts: 291
Quote:
Originally Posted by
lukeirvin
makes much more sense, but the image is still always showing
Is the text field hooked up correctly?
01-31-2012, 11:36 PM
#5 (permalink )
Registered Member
Join Date: Jun 2010
Posts: 210
Yes I believe so. Should string be a property and synthesized?
If I do this [string length] == 0, then the image never shows no matter what
But if i try != or > it always shows
01-31-2012, 11:46 PM
#6 (permalink )
Registered Member
Join Date: May 2009
Location: Stanford, CA
Posts: 291
oh sorry. String should be a local variable. (Also I don't like the variable name string)
Code:
NSString *text = [NSString stringWithFormat:@"%@", aTextField.text]
if ([text length] != 0) {
}
01-31-2012, 11:52 PM
#7 (permalink )
Registered Member
Join Date: Jun 2010
Posts: 210
still always shows image
01-31-2012, 11:55 PM
#8 (permalink )
Registered Member
Join Date: May 2009
Location: Stanford, CA
Posts: 291
Ok let's try a different tactic:
Code:
if ([aTextField.text isEqualToString:@""]) {
} else {
}
02-01-2012, 06:55 PM
#9 (permalink )
Registered Member
Join Date: Jun 2010
Posts: 210
got it working! Used that method and had to change one line of code.
Thank you much for you help
02-01-2012, 07:09 PM
#10 (permalink )
Registered Member
Join Date: Jun 2010
Posts: 210
Found a new issue:
If I leave the text field empty no QR Image appears
If I enter in text, a QR Image will appear. If I go back and remove the text then proceed, the QR Image is still there.
Everything I have tried so far hasn't fixed this... Tried releasing it in a couple of places but that was not the case.
Thoughts?
02-01-2012, 07:47 PM
#11 (permalink )
Cocoa Junkie
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Quote:
Originally Posted by
lukeirvin
Found a new issue:
If I leave the text field empty no QR Image appears
If I enter in text, a QR Image will appear. If I go back and remove the text then proceed, the QR Image is still there.
Everything I have tried so far hasn't fixed this... Tried releasing it in a couple of places but that was not the case.
Thoughts?
Add a log statement:
Code:
NSLog(@"aTextField.text = '%@'. length = %d", aTextField.text, [aTextField.text length]);
Then your if statement:
Code:
if ([aTextField.text length] != 0)
{
//generate a QR code
}
else
{
clear out the QR code
}
Then run the program and see what you are getting in the console log when you type in something, then try to erase it.
That will let you figure out what's going wrong.
02-01-2012, 08:28 PM
#12 (permalink )
Registered Member
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
i can't tell if you are saying your triggering code is off or your "make image disappear" code is not working, but to make an image disappear I usualy use this line:
aImageView.alpha=0;
02-01-2012, 09:58 PM
#13 (permalink )
Registered Member
Join Date: Jun 2010
Posts: 210
malloc: *** error for object 0x8ac0260: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
02-01-2012, 10:01 PM
#14 (permalink )
Registered Member
Join Date: Jun 2010
Posts: 210
Never mind on that one. I was releasing the object too many times.
Looking great now! Thanks for the help
02-01-2012, 11:29 PM
#15 (permalink )
iPhone Developer
Join Date: Dec 2008
Location: India
Age: 28
Posts: 156
comparison operator
I don't know if you did it knowingly but in if loop you were using assignment operator = not comparison operator == so it will assign something like empty string to string variable and the statement will be true so it will always go in.
02-02-2012, 08:56 AM
#16 (permalink )
Registered Member
Join Date: Jun 2010
Posts: 210
Right, I knew to avoid that
One last question. The image is being generated when I want and it looks great on screen. But when I email it or save it the image becomes blurry. Has anyone had an issue like this before? I tried doing the same method as before to make the image sharper but that did not work :-/
02-02-2012, 09:02 AM
#17 (permalink )
Cocoa Junkie
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Quote:
Originally Posted by
RickSDK
i can't tell if you are saying your triggering code is off or your "make image disappear" code is not working, but to make an image disappear I usualy use this line:
aImageView.alpha=0;
You should use aImageView.hidden=true instead.
That takes disables it and cleanly hides it, rather than forcing the graphics engine to composite the image with the surroundings, like it has to do when the alpha is not 1.0
02-03-2012, 05:28 AM
#19 (permalink )
Cocoa Junkie
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Quote:
Originally Posted by
lukeirvin
Right, I knew to avoid that
One last question. The image is being generated when I want and it looks great on screen. But when I email it or save it the image becomes blurry. Has anyone had an issue like this before? I tried doing the same method as before to make the image sharper but that did not work :-/
Post the code that you are using to export the image. My guess is that you are saving it to a different size and/or JPEG compressing it, both of which will result in a loss of image quality.
02-03-2012, 05:50 PM
#20 (permalink )
Registered Member
Join Date: Jun 2010
Posts: 210
This makes my QR image:
Code:
UIImage * image = [QREncoder encode:aTextField.text];
aImageView = [[UIImageView alloc] initWithImage:image];
CGFloat aSize = self.view.bounds.size.width - kPadding * 13;
aImageView.frame = CGRectMake(605, 20, 100, 100);
aImageView.frame = CGRectMake(kPadding, (self.view.bounds.size.height - aSize) / 2, aSize, aSize);
[aImageView layer].magnificationFilter = kCAFilterNearest;
aImageView.center = CGPointMake(680, 80);
[secondVeiwController.view addSubview:aImageView];
In my second view a user can email a PDF attachment that has the image among other information in it. The image becomes blurry in the PDF
02-04-2012, 05:41 PM
#21 (permalink )
Registered Member
Join Date: Jun 2010
Posts: 210
Thoughts on this?
Do I need to be doing any image code when making my PDF?
02-04-2012, 06:12 PM
#22 (permalink )
Registered Member
Join Date: Jun 2010
Posts: 210
This is where I make my PDF (forgot to include this):
Code:
NSMutableData * pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, self.view.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[secondViewController.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 421
16 members and 405 guests
Atatator , chiataytuday , condor304 , dre , FrankWeller , imac74 , ipodphone , jeroenkeij , kukat , LunarMoon , n00b , PowerGoofy , QuantumDoja , Retouchable , tim0504 , VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,675
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Retouchable