滚动视图中的图像

我正在使用以下代码尝试在UIScrollView中显示图像。

[self.jarView addSubview:imgView]; 

这是行不通的。 但是,当我将子视图添加到普通视图时,它就起作用了。 我的UIScrollView被称为jarView,imgView是一个UIImageView。

谢谢

这对我来说很好:

 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]; 

最后定义多less滚动scrollview滚动:

 scrollview.contentSize = CGSizeMake(width,height); 

确保您的scrollView.contentSize属性值设置正确(宽度和高度应该> 0)。

在这里,我张贴我的答案。 尝试一下。

您可以将所有图像加载到数组中。 你可以devise一个图像视图,并尝试下面的代码:

数组名称:examplearray和视图名称: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); } 

我给你我的示例代码。 在这段代码中,我在滚动视图中添加了多个图像,这对我来说很好:

 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]; } 

我希望这有帮助。