2011-11-24 11 views
2

다음 코드를 사용하여 UIScrollView에 이미지를 표시하려고합니다.Image In Scroll View

[self.jarView addSubview:imgView]; 

작동하지 않습니다. 그러나, 내 기본보기에 하위보기를 추가 할 때 작동합니다. 내 UIScrollView는 jarView이고 imgView는 UIImageView입니다.

감사

+0

가 기본보기로 작동하는 경우입니다 '. 속성, 콘센트, 초기화 메소드를 확인하십시오. – beryllium

+0

@beryllium init 메서드는 무엇입니까 ?? ScrollView를 초기화해야합니까? 이미지가 제대로 초기화되었습니다. 스크롤 뷰는 Interface Builder에서 생성됩니다. –

+0

스크롤바가 인터페이스 빌더의 IBOutlet jarView에 링크되어 있습니까? –

답변

1

이 나를 위해 잘 작동 :

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

에서 마지막 scrollview이 스크롤이 얼마나 정의 :

scrollview.contentSize = CGSizeMake(width,height); 
+0

2) ScrollView와 View는 새로운 것입니까, 아니면 기존 IB 객체입니까? 2) y1, x1, y2, x2는 ScrollView와 동일합니까? 덕분에 –

+0

여기 내가 동적으로 프로그래밍 ...... 나는 scrollview을 정의하고 ... 그것에 대한 메모리를 할당 ... 2) y1, x1, y2, x2는 내가 scrollview에서 좀 덜 받아 좌표 (그래서 사용자가 스크롤 할 수 있습니다) .....하지만 (높이보기 = 이미지 높이) ...이 게시물은 당신에게 도움이 .... 잊지 마세요 내 대답을 수락 .... 당신은 항상 환영합니다 ..:) – GauravBoss

+0

나는 하나의 코드 라인이 for 루프에있는 다중 이미지를 가지고있다. 여전히 작동합니까? (이미지가 겹치지 않음) –

0

scrollView.contentSize 속성 값이 올바르게 설정되어 있는지 확인 (폭 높이> 0).

+0

작동하지 않습니다. . . –

0

샘플 코드를 제공합니다. 이 코드에서 스크롤 뷰에 여러 이미지를 추가하면 다음과 같이 잘 작동합니다.

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

이 정보가 도움이되기를 바랍니다.

0

여기 내 대답을 게시합니다. 시도 해봐.

모든 이미지를 배열에로드 할 수 있습니다. 그리고 당신은 하나의 이미지 뷰를 갖는 뷰를 ​​설계하고 아래 코드를 시도 할 수 있습니다 :

는 배열 이름

: examplearray 및보기 이름 : 다음 문제는`self.jarView에 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); 
}