Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-31-2012, 10:23 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default 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?
lukeirvin is offline   Reply With Quote
Old 01-31-2012, 10:41 PM   #2 (permalink)
Registered Member
 
msencenb's Avatar
 
Join Date: May 2009
Location: Stanford, CA
Posts: 291
msencenb is on a distinguished road
Default

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) {

}
__________________
I'm starting a new blog dedicated to iOS development. Check it out at:

http://www.iosdevscreencasts.com
msencenb is offline   Reply With Quote
Old 01-31-2012, 11:23 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default

makes much more sense, but the image is still always showing
lukeirvin is offline   Reply With Quote
Old 01-31-2012, 11:26 PM   #4 (permalink)
Registered Member
 
msencenb's Avatar
 
Join Date: May 2009
Location: Stanford, CA
Posts: 291
msencenb is on a distinguished road
Default

Quote:
Originally Posted by lukeirvin View Post
makes much more sense, but the image is still always showing
Is the text field hooked up correctly?
__________________
I'm starting a new blog dedicated to iOS development. Check it out at:

http://www.iosdevscreencasts.com
msencenb is offline   Reply With Quote
Old 01-31-2012, 11:36 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default

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
lukeirvin is offline   Reply With Quote
Old 01-31-2012, 11:46 PM   #6 (permalink)
Registered Member
 
msencenb's Avatar
 
Join Date: May 2009
Location: Stanford, CA
Posts: 291
msencenb is on a distinguished road
Default

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) {

}
__________________
I'm starting a new blog dedicated to iOS development. Check it out at:

http://www.iosdevscreencasts.com
msencenb is offline   Reply With Quote
Old 01-31-2012, 11:52 PM   #7 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default

still always shows image
lukeirvin is offline   Reply With Quote
Old 01-31-2012, 11:55 PM   #8 (permalink)
Registered Member
 
msencenb's Avatar
 
Join Date: May 2009
Location: Stanford, CA
Posts: 291
msencenb is on a distinguished road
Default

Ok let's try a different tactic:

Code:
if ([aTextField.text isEqualToString:@""]) {

} else {

}
__________________
I'm starting a new blog dedicated to iOS development. Check it out at:

http://www.iosdevscreencasts.com
msencenb is offline   Reply With Quote
Old 02-01-2012, 06:55 PM   #9 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default

got it working! Used that method and had to change one line of code.

Thank you much for you help
lukeirvin is offline   Reply With Quote
Old 02-01-2012, 07:09 PM   #10 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default

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?
lukeirvin is offline   Reply With Quote
Old 02-01-2012, 07:47 PM   #11 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by lukeirvin View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-01-2012, 08:28 PM   #12 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

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;
__________________
Check out my apps

RickSDK is offline   Reply With Quote
Old 02-01-2012, 09:58 PM   #13 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default

malloc: *** error for object 0x8ac0260: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
lukeirvin is offline   Reply With Quote
Old 02-01-2012, 10:01 PM   #14 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default

Never mind on that one. I was releasing the object too many times.

Looking great now! Thanks for the help
lukeirvin is offline   Reply With Quote
Old 02-01-2012, 11:29 PM   #15 (permalink)
iPhone Developer
 
Narender Mudgal's Avatar
 
Join Date: Dec 2008
Location: India
Age: 28
Posts: 156
Narender Mudgal is on a distinguished road
Send a message via MSN to Narender Mudgal Send a message via Skype™ to Narender Mudgal
Smile 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.
Narender Mudgal is offline   Reply With Quote
Old 02-02-2012, 08:56 AM   #16 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default

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 :-/
lukeirvin is offline   Reply With Quote
Old 02-02-2012, 09:02 AM   #17 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by RickSDK View Post
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
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-02-2012, 10:25 PM   #18 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default

Still haven't found a solution to my blurry image.

I'm having the same issue this developer is having:

objective c - kCAFilterNearest maginifcation filter (UIImageView) - Stack Overflow

I have tried the recommended method:

Code:
    aImageView.frame = CGRectIntegral(aImageView.frame);
but no luck. Any thoughts on what to try?
lukeirvin is offline   Reply With Quote
Old 02-03-2012, 05:28 AM   #19 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by lukeirvin View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-03-2012, 05:50 PM   #20 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default

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
lukeirvin is offline   Reply With Quote
Old 02-04-2012, 05:41 PM   #21 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default

Thoughts on this?

Do I need to be doing any image code when making my PDF?
lukeirvin is offline   Reply With Quote
Old 02-04-2012, 06:12 PM   #22 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default

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();
lukeirvin is offline   Reply With Quote
Reply

Bookmarks

Tags
uiimage, uiimageview, uiviewcontroller

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» 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
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:02 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0