ViewWillAppear Won't Load Image.

Discussion in 'iOS Programming' started by loon3y, Apr 10, 2012.

  1. loon3y macrumors 65816

    loon3y

    Joined:
    Oct 21, 2011
    #1
    i have a barcode scanning app, and i have an image view showing the picture of the recently scanned item, through a web service. it worked in the beginning, but after i changed something a while back it stopped showing up. now I'm trying to fix this problem. in only appears when i go out of the view controller (via tab bar) and go back in, here is my viewWillAppear code:







    Code:
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        
        // use the product code for image, even if UPC is scanned
        NSString *sURL = [[NSString alloc] initWithString:[NSString stringWithFormat:@"http://******.*****.com:90/prodimage/%@.jpg", displayText.text]];
        NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",sURL]]];
        UIImage *image = [[UIImage alloc] initWithData:mydata];
        [invenImage setImage:image];
    }
     
  2. cMacSW, Apr 10, 2012
    Last edited by a moderator: Apr 10, 2012

    cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #2
    Try telling the image view needsDisplay
    Code:
    [yourImageView setNeedsDisplay:YES];
     
  3. loon3y thread starter macrumors 65816

    loon3y

    Joined:
    Oct 21, 2011
    #3



    thanks but it didnt work, i running Xcode 4.3.1, so i don know if its the new version but there isn't a boolean after the code in my Xcode.





    is there a method or function where i can refresh/reload the image that i can add at the end of viewWillAppear?
     
  4. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #4
    That's what setNeedsDisplay should do.
     
  5. loon3y thread starter macrumors 65816

    loon3y

    Joined:
    Oct 21, 2011
    #5


    damn it, it worked before no problem, but after a co worker changed some coding in another view controller (using global variables) it stopped working.
     
  6. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #6
    Did you confirm that this is being called the first time the view is displayed by setting a breakpoint?

    ----------

    Also, not related to your problem, but this line:

    Code:
    NSString *sURL = [[NSString alloc] initWithString:[NSString stringWithFormat:@"http://******.*****.com:90/prodimage/%@.jpg", displayText.text]];
    could be changed to this:

    Code:
    NSString *sURL = [NSString stringWithFormat:@"http://******.*****.com:90/prodimage/%@.jpg", displayText.text];
     
  7. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #7
    cMacSW - I was reading this thread and was also wondering in the begining why he did not just use 'stringWithFormat'.

    loon3y - When I develop problems like this I NSLog after every line of code to check to make sure my results are what I expected them to be.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    I'd suggest it's easier to just set a breakpoint and step through / into every line of code.
     
  9. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #9
    The docs says there are situations where viewWillAppear will not be called, that's why setting a break point may help here.
     
  10. loon3y, Apr 17, 2012
    Last edited: Apr 17, 2012

    loon3y thread starter macrumors 65816

    loon3y

    Joined:
    Oct 21, 2011
    #10
    Resolved:



    basically i shouldn't of called the image in my barcode function or viewWillAppear or viewDidAppear.






    i had to put it in my XML parse section of the code, because when I'm calling the image I'm using a store specified product code rather than calling it by the UPC when i scan. ( i know it should be either or but everything is mickey mouse and going to cleanup/optimize it later)






    Code:
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
    {
        
        if ([elementName isEqualToString:@"itemno"])
        {
            displayText.text = capturedCharacters;
            
            // display the image on 1st viewcontroller
            NSString *sURL = [[NSString alloc] initWithString:[NSString stringWithFormat:@"http://*****.******.com:90/prodimage/%@.jpg", capturedCharacters]];
            NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",sURL]]];
            UIImage *image = [[UIImage alloc] initWithData:mydata];
            [invenImage setImage:image];
            [invenImage setNeedsDisplay];
        }
     
        [capturedCharacters release];
        capturedCharacters = nil;
        
        if ([elementName isEqualToString:@"str_partinfo"]) {
            // We are no longer in an item element
            inItemElement = NO;
        }
    }
    


    hope this helps out anyone that gets confused using lineapro
     

Share This Page