繁体   English   中英

UIImageView 未出现在 UIScrollView 内部

[英]UIImageView not showing up inside of a UIScrollView

我有一个 iPhone 应用程序,它应该在 UIScrollView 中显示图像。 我认为我可以使用 Interface Builder 手动添加 UIScrollView,然后使用 UIImageView 添加图像。

我是在错误地接近这个吗? 例如,我应该直接将图像添加到 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 工作得很好。 所以这就是为什么我想知道我是否正在接近这个错误,或者我是否只是在我的编码中犯了一个错误。

能否请你帮忙? 因为在过去的几个小时里我一直在做这件事,而且还没有更进一步。

更新 - 这是我根据 Chris 的建议所做的。 它对我有用,但如果仍有任何问题,请指出。

        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;

在此代码区域中检查您发送到 scrollImageView 的帧:

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

您正在使用滚动条的框架来放置 object,但滚动条的框架将是其父视图中的坐标。 因此,通过混合不同坐标集,您很可能将 scrollImageView 定位在滚动条的内容区域之外。

您可能想改用 scroller.bounds ,它是滚动条内容区域的矩形。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM