UIScrollView not showing the image!

Discussion in 'iOS Programming' started by noahgolm, Feb 17, 2011.

  1. noahgolm macrumors member

    Joined:
    Aug 18, 2010
    #1
    Okay, so I'm developing my own personal xkcd app (I friggin' love that comic). As some of you may know, the creator made a JSON script with the newest comic for ease of use. I have successfully parsed that and, when I use a UIImageView, can show the semi-daily comic. Because of the change of comic size and lack of maneuverability, however, I wanted to transfer this image to a UIScrollView. I thought it would be as simple as adding the imageView as a subview of the scrollView, but the image never actually shows up when that happens! Here's my code... (I have the IBOutlet and properties declared in the .h file, so that's not the problem)

    Code:
    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:imager]; // imager is the actual image
    	self.imageView = tempImageView;
    	[tempImageView release];
    	
    	scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
    	scrollView.maximumZoomScale=4.0;
    	scrollView.minimumZoomScale=0.75;
    	scrollView.clipsToBounds=YES;
    	scrollView.delegate=self;
    	[scrollView addSubview:imageView];
    
    I tried adding the "tempImageView" directly as a subview as well, but still no results! What could be happening here?
     
  2. nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #2
    Did you make an IBOutlet for scrollView? Did you connect it in IB?

    Nick
     
  3. noahgolm thread starter macrumors member

    Joined:
    Aug 18, 2010
    #3
    Yes, I added it (as stated earlier)...
    Code:
    IBOutlet UIScrollView *scrollView;
    
    and then...
    Code:
    @property(nonatomic,retain)UIScrollView *scrollView;
    
    It's also hooked up to a proper UIScrollView in IB. Any ideas on what's going on here? The setup with JSON and the tempImageView works when just displaying an UIImageView, so it has to be something with the scrollView.
     
  4. noahgolm thread starter macrumors member

    Joined:
    Aug 18, 2010
    #4
    In fact, I'll put up the project, just in case someone wants to look at it. Tis in the attachment!
     

    Attached Files:

    • xkcd.zip
      File size:
      799.8 KB
      Views:
      67
  5. nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #5
    I don't see the image included in the project folder you attached.
     
  6. noahgolm thread starter macrumors member

    Joined:
    Aug 18, 2010
    #6
    If you look through the .m file, you'll see that the image itself is gathered from a link (as in the image has "dataWithContentsOfURL"). The link is gathered from the JSON. Again, this works with just displaying the UIImageView. Strange!
     
  7. nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #7
    I thumbed around with it a bit but still nothing. I never got the content of the URL to show up. It might just be a simulator thing. Good luck finding your answer!

    Nick
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    What's the frame of the imageView?

    Are you sure that the image isn't nil?
     
  9. noahgolm thread starter macrumors member

    Joined:
    Aug 18, 2010
    #9
    Here's a newer version of the project. Same problem!
     

    Attached Files:

  10. noahgolm, Feb 17, 2011
    Last edited: Feb 17, 2011

    noahgolm thread starter macrumors member

    Joined:
    Aug 18, 2010
    #10
    The image is not nil. I use the line
    Code:
    imager = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageString]]];
    where "imager" is the UIImage, and "imageString" is a string containing the picture's URL ("http://imgs.xkcd.com/comics/wisdom_teeth.png" so far, but changes as the comic updates).

    The frame is made with this...
    Code:
    CGRect frame = CGRectMake(0, 0, imager.size.width, imager.size.height);
    	[imageView initWithFrame:frame];
     
  11. PhoneyDeveloper, Feb 17, 2011
    Last edited: Feb 17, 2011

    PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    I downloaded your sample code. Ran it. And I see a cartoon in the Sim.

    So what's the problem again?

    I turned the scrollview back on and that works too.
     

    Attached Files:

  12. noahgolm thread starter macrumors member

    Joined:
    Aug 18, 2010
    #12
    OK, I just ran it off of your attachment, and it actually works. That seems... Weird. I did notice you changed a few lines of code, though. Also, in IB, it looks like you have the UIImageView on top of the UIScrollView. Also, there was no subview of scrollView added. Was the scrolling just used as a way of viewing the entire part of the screen that the image was part of? That's something I wouldn't have thought of. Thank you! The app works exactly as hoped for!
     
  13. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #13
    I added a scrollview and set the imageview as its subview in IB. It worked without the scrollview, but the cartoon was scaled to fit the imageView. With the scrollview the imageview is the same size as the image and you scroll to see the whole image.

    Um, what else? That's what a scrollview is for.

     

Share This Page