1

我有以下代码,它采用包含 uiimage 路径的 nsdictionaries 数组,并将它们一一添加到 uiscrollview。

- (void)displayAssets:(NSArray*)postAssets //configured for search
{
    for (UIView *v in self.scrollview.subviews) {
        [v removeFromSuperview];
    }

    CGRect workingFrame = self.scrollview.frame;
    workingFrame.origin.x = 0;

    self.scrollview.delegate = self;
    //    self.scrollview.contentInset=UIEdgeInsetsMake(10.0,0.0,10.0,0.0);

    NSString *pathOfNoImageFile = [[NSBundle mainBundle] pathForResource:@"noimage" ofType:@"png"];

    if (postAssets == nil || [postAssets count] == 0) {

        UIImage *noImage = [UIImage imageWithContentsOfFile:pathOfNoImageFile];
        UIImageView *imageview = [[UIImageView alloc] initWithImage:noImage];

        [imageview setContentMode:UIViewContentModeScaleAspectFit];
        imageview.frame = workingFrame;
        [self.scrollview addSubview:imageview];
    }
    else
    {

            //at this moment we have an actual NSArray/NSDictionarry of images that has keys etc.

            for (NSDictionary *asset in postAssets) //we have to differentiate between images and videos in future
            {

                //ASSUMING FIRST VALUE IN ASSETS IS AN Image   THIS WILL NEED REFACTORING FOR VIDEOS LATER
                NSString * assetID = [asset valueForKeyPath:@"id"];
                NSString * imageURL = [NSString stringWithFormat:@"%@post/7777/images/%@.jpg", WEB_SERVICE_BASE_URL, assetID];

                UIImage *waitingImage = [UIImage imageWithContentsOfFile:pathOfNoImageFile];
                UIImageView *imageview = [[UIImageView alloc] init];

                [imageview  setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:waitingImage];

                [imageview setContentMode:UIViewContentModeScaleAspectFit];
                imageview.frame = workingFrame;

                [self.scrollview addSubview:imageview];
                workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
            }
            [self.scrollview setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];



    }

    //    [[self tableView] reloadData];

}

我在同一文件的另一个功能中尝试做的是获取第一个 uiimage 或当前显示的 uiimage 并且无法这样做。uiscrollview 似乎没有一种方法可以让人们深入了解各个视图,以便我可以获取 uiimage。我看过其他 SO 问题,但它们似乎很复杂。

有人可以指导我吗?我对 uiscrollviews 相当陌生。

谢谢

4

1 回答 1

0

假设每个图像与滚动视图的宽度相同,您可以:

  1. 保留指向您添加的 UIImages 的指针数组,然后使用获取当前显示图像的索引scrollview.contentOffset.x / scrollview.bounds.size.width

  2. 调用hitTest:(CGPoint)point withEvent:(UIEvent *)event使用scroller.contentOffset作为点 - 这应该返回此时应该是你的 UIImageView 的最上面的视图

于 2013-03-30T21:47:47.647 回答