Reputation: 355
I have an iPhone application that is supposed to show images inside a UIScrollView. I thought that I could add the UIScrollView manually using Interface Builder and then add images using UIImageView.
Am I approaching this incorrectly? For example, should I just be adding images to the UIScrollView directly?
Here's my code:
- (void)request:(FBRequest *)request didLoad:(id)result {
//Code for array of photos
NSArray *photoAlbumArray=(NSArray*)[result valueForKey:@"data"];
arrayCount=[photoAlbumArray count];
//check to see if I did that correctly
[self.label setText:[NSString stringWithFormat:@"%i", arrayCount]];
//should have an array of photo objects and the number of objects, correct?
scrollWidth = 0;
scroller.contentSize=CGSizeMake(arrayCount*scroller.frame.size.width, scroller.frame.size.height);
for (int i = 0; i < arrayCount;i++) {
CGRect rect = scroller.frame;
rect.size.height = scroller.frame.size.height;
rect.size.width = scroller.frame.size.width;
rect.origin.x = scroller.frame.origin.x + scrollWidth;
rect.origin.y = scroller.frame.origin.y;
UIImageView *scrollImageView = [[UIImageView alloc] initWithFrame:rect];
id individualPhoto = [photoAlbumArray objectAtIndex:i];
NSLog(@"%@",individualPhoto);
NSArray *keys=[individualPhoto allKeys];
NSLog(@"%@",keys);
NSString *imageURL=[individualPhoto objectForKey:@"source"];
//here you can use this imageURL to get image-data and display it in imageView
NSURL *url = [NSURL URLWithString:imageURL];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
//check to make sure the proper URL was passed
[self.label setText:imageURL];
scrollImageView.image = img;
//I have an imageView next to the UIScrollView to test whether that works - it does.
imageView.image = img;
[scroller addSubview:scrollImageView];
[img release];
[scrollImageView release];
scrollWidth += scroller.frame.size.width;
}
pageControl.numberOfPages=arrayCount;
pageControl.currentPage=0;
}
Based on this call:
-(IBAction)showAlbum:(UIButton *)sender
{
//Go goes here to get an album and display it in the UIScrollView
[_facebook requestWithGraphPath:@"ALBUM_ID/photos" andDelegate:self];
}
I'm displaying the image in a UIImageView to the left of the UIScrollView (for testing purposes) and the UIImageView works just fine. So that's why I'm wondering if I'm approaching this wrong or if I just made a mistake in my coding.
Could you please help? Because I've been working on this for the past few hours and haven't gotten any further.
UPDATE - here is what I did based on Chris' recommendation. it works for me but if there are any issues still please do point them out.
UIImageView *scrollImageView = [[UIImageView alloc] initWithFrame:[scroller bounds]];
CGRect rect = scrollImageView.frame;
rect.origin.x = scrollImageView.frame.origin.x + scrollWidth;
rect.origin.y = scrollImageView.frame.origin.y;
scrollImageView.frame = rect;
Upvotes: 1
Views: 2512
Reputation: 484
Check the frame you're sending to scrollImageView, in this area of code:
rect.size.height = scroller.frame.size.height;
rect.size.width = scroller.frame.size.width;
rect.origin.x = scroller.frame.origin.x + scrollWidth;
rect.origin.y = scroller.frame.origin.y;
UIImageView *scrollImageView = [[UIImageView alloc] initWithFrame:rect];
You're using the scroller's frame to place the object, but the scroller's frame will be IT'S coordinates in it's PARENT view. So by mixing sets of different coordinates, you're most likely positioning the scrollImageView outside the scroller's content area.
You probably want to use scroller.bounds instead, which is the rectangle for the scroller's content area.
Upvotes: 1