2

I am using the following code to try and display an image in a UIScrollView.

[self.jarView addSubview:imgView];

It does not work. However, it works when I add the subview to my normal view. My UIScrollView is called jarView and imgView is a UIImageView.

Thanks

7
  • If it works with normal view, then problem is in self.jarView. Check your property, outlet, init method. Commented Nov 24, 2011 at 17:21
  • @beryllium What is the init method?? Does the ScrollView need to be initialized? My image is properly initialized. The scrollview is created in Interface Builder. Commented Nov 24, 2011 at 18:03
  • Is the scrollview linked to the IBOutlet jarView in interface builder? Commented Nov 24, 2011 at 18:59
  • @JacobJennings,Yes, but what is the delegate in the scrollview? Commented Nov 24, 2011 at 19:00
  • The delegate should not affect the ability to add subviews. developer.apple.com/library/IOs/#documentation/UIKit/Reference/… Commented Nov 24, 2011 at 19:03

4 Answers 4

1

This works fine for me:

UIScrollView *scrollview;
scrollview= [[UIScrollView alloc] initWithFrame:CGRectMake(,,,)];// set scroll view size
view = [[UIView alloc] initWithFrame:CGRectMake(y1,x1,y2,x2)];  //set the view size
UIImage *image1 = [UIImage imageNamed:imageName];
image=[[UIImageView alloc]initWithImage:image1];// take image size according to view
[view addSubview:image];
[scrollview addSubview:view];
[view release];

At last define how much scrollview is scroll:

scrollview.contentSize = CGSizeMake(width,height);
Sign up to request clarification or add additional context in comments.

4 Comments

2 questions: 1) Is ScrollView and View, new or are they the existing IB objects? 2) Is y1,x1,y2,x2 the same as in the ScrollView? Thanks
here i do all programming dynamically...... i define scrollview..and allocate memory for it.... 2) y1,x1,y2,x2 coordinates i take little less from scrollview (so user can scroll).....but (view height = image height)... if this post helpful to you....don't forget Accept my answer.... you welcome always..:)
I have multiple images, that single line of code is in a for loop. Will it still work?(The images don't overlap)
What is view? is it self.view or is it created dynamically?
0

Make sure that your scrollView.contentSize property value is set correctly (width and height should be > 0).

Comments

0

Here I am posting my answer. Try it.

You can load all the images to an array. And you can design a view having one image view and try the below code:

array name: examplearray and view name :exampleview

-(void)updateImagesInScrollView
{
     int cnt = [examplearray count];

    for(int j=0; j< cnt; ++j)
    {
        NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"exampleview" 
                                                             owner:self 
                                                           options:nil];

        UIView *myView = [nibContents objectAtIndex:0];
        exampleview * rview= (exampleview *)myView;
        /* you can get your image from the array and set the image*/ 
        rview.imageview.image = yourimage;
        /*adding the view to your scrollview*/
        [self.ScrollView addSubview:rview];
    }
    /* you need to set  the content size of the scroll view */
    self.ScrollView.contentSize = CGSizeMake(X, self.mHorizontalScrollView.contentSize.height);
}

Comments

0

I am giving you my sample code. In this code i add multiple images on scroll view and this work fine for me:

UIScrollView *scrollview;

- (void)anyFunction
{
    scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, 320, 50)];   
    NSInteger viewcount = [cat_Products count]; // if you have four content your view count is 4..(four)..in my case i fetch it from database

    CGFloat y2 = 0;
    UIView *view;
    UIImageView *imageView;
    NSString *imageName;

    for(int i = 0; i< viewcount; i++)   
    {  
        CGFloat y = i * 64;  
        y2 = y2 + 64;

        view = [[UIView alloc] initWithFrame:CGRectMake(y, 0, y2, 50)];

        imageName = [[NSString alloc] initWithFormat:@"c%i", (i + 1)]; // my image name is c1, c2, c3, c4 so i use this

        UIImage *image1 = [UIImage imageNamed:imageName];   
        imageView = [[UIImageView alloc] initWithImage:image1];
        [view addSubview:imageView];
        [scrollview addSubview:view];
        [view release];
        [imageView release];
        [imageName release];
    }    

scrollview.contentSize = CGSizeMake(viewcount*64,0);  //viewcount*64 because i use image width 64..and  second parameter is 0..because i not wants vertical scrolling... 
[myView addSubview:scrollview];
[scrollview release];
}

I hope this helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.