2011-07-08 8 views
1

私は、UIScrollView内に画像を表示するはずのiPhoneアプリケーションを持っています。 Interface Builderを使用してUIScrollViewを手動で追加し、UIImageViewを使用して画像を追加できると思っていました。UIImageViewがUIScrollViewの内部に表示されない

これは間違っていますか?たとえば、UIScrollViewにイメージを直接追加するだけですか?

は、ここに私のコードです:この呼び出しに基づいて

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

-(IBAction)showAlbum:(UIButton *)sender 
{ 
    //Go goes here to get an album and display it in the UIScrollView 
    [_facebook requestWithGraphPath:@"ALBUM_ID/photos" andDelegate:self];  
} 

私は(テスト目的のために)UIScrollViewのとUIImageViewの左にUIImageViewで画像を表示していますうまく動作します。だからこそ私はこの間違いに近づいているのか、まさに私のコーディングで間違いを犯したのか疑問に思います。

助けてください。私は過去数時間このことに取り組んできたので、それ以上は得られていないからです。

UPDATE - ここで私はChrisの推薦に基づいています。それは私のために働くが、それでも問題がある場合は、それらを指摘してください。

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

あなたは、オブジェクトを配置するためにスクロールバーのフレームを使用しているが、スクロールバーのフレームは、ITの座標になります。

 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; 
+0

CGRectOffset関数を使用すると、事をさらに単純化できます。 「initWithFrame:CGRectOffset(scroller.bounds、scrollWidth、0)」 – Chris

+0

何が起きているのかを確認するためにドキュメントをチェックアウトしました。私はそれについて全く知らなかった。もう一度@Chrisにお返事します:-) – NateHill

答えて

1

は、コードのこの地域では、scrollImageViewに送っているフレームをチェックそれは親のビューです。したがって、異なる座標のセットをミキシングすることで、scrollImageViewをスクロールのコンテンツ領域外に配置する可能性が高くなります。

代わりにscroller.boundsを使用します。これは、スクロール側のコンテンツ領域の矩形です。

+0

そして、私の友人は、なぜ@Chrisが大金を払うのです!ありがとうございました:-)他の誰かがこの問題に遭遇した場合、私はChrisの勧告に基づいて自分のコードを更新しました。 – NateHill

関連する問題