User558
User558

Reputation: 1175

How to add a UIImageVIew in UIScrollView Programatically in iOS?

In my application I want to display a images in UIScrollview. Here I'm not getting the scrollview height and width as image height and width.How to get scrollview(Hight,Width) to my image view(height and width)? Here is upto now I had tried.

for (int i=0; i<imageArray.count; i++)
{
    NSString *str = [imageArray objectAtIndex:i];
    NSString *bannerImageURL = [NSString stringWithFormat:@"http:url/%@", str];
    CGFloat myOrigin = i * self.view.frame.size.width;
    UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width,imgScrollView.frame.size.height)];
    myImageView.contentMode = UIViewContentModeScaleAspectFit;
    myImageView.clipsToBounds = YES;
    myImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:bannerImageURL]]];

    imgScrollView.contentSize = CGSizeMake(self.view.frame.size.width * imageArray.count,                                                               self.view.frame.size.height);


    CGPoint scrollPoint = CGPointMake(0, 0);

    [imgScrollView setContentOffset:scrollPoint animated:YES];
    imgScrollView.delegate = self;
    [imgScrollView addSubview:myImageView]; 

Here is my Delegate Methods.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[imgScrollView setContentOffset: CGPointMake(imgScrollView.contentOffset.x,0)];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
NSLog(@"Scrolling - You are now on page %i",page);
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                 withVelocity:(CGPoint)velocity
  targetContentOffset:(inout CGPoint *) targetContentOffset
NS_AVAILABLE_IOS(5_0)
{
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
NSLog(@"Dragging - You are now on page %i",page);
}

What I did mistake in my Code? Please Help me.

Upvotes: 0

Views: 935

Answers (1)

Luke Smith
Luke Smith

Reputation: 1320

Theres several problems I can see. First the line

myImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:bannerImageURL]]];

It might work but there are way too many nested calls on that line. You can't be sure which, if any, fail, if it happens. Separate it into 3 separate calls, so that you can step through and be sure its all working.

This line :

imgScrollView.contentSize = CGSizeMake(self.view.frame.size.width * imageArray.count,

Theres a Y value missing (probably paste issue) but also - you dont need to do this inside your for loop, you can just do it when you have finished adding all images. Same for setting the delegate - you dont need to do it every time. And setting the scroll point. All that should happen after the for loop.

Most importantly though, your implementation of the method scrollViewDidScroll has a call to setContentOffset. That will definitely not work. The call you make has the effect of scrolling the scrollview, which will therefore call the scrollViewDidScroll method again - you see the problem.

Most likely reason why your images arent appearing though is the first issue I mention - most likely your URL's are wrong somewhere, so you need to debug that by stepping through separate calls.

Upvotes: 1

Related Questions