IOS。 ScrollView只显示一个图像的子视图

我有一个UIScrollView,我想从flickr加载一个图像列表。 然后,我希望这些照片能够像原始股票iphone照片应用程序一样显示,以便在照片中滚动。

这是我的代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    for (int i = 0; i < self.urlArray.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];


        [self returnImageFromFlickr:[self.urlArray objectAtIndex:i]];

        UIImageView *imageView = self.largePhoto;

        [subview addSubview:imageView];
        [self.scrollView addSubview:subview];

    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * self.urlArray.count, self.scrollView.frame.size.height);
}

-(void)returnImageFromFlickr:(NSURL *)url {



    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    [self.largePhoto setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"placeholder.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {



        [self.largePhoto setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

    }];

}

正在显示的只是最后一张图片。

我根据自己的理解尝试了解决方案,并获得了这一点。 这不是摄影照片。 只是一个白色的屏幕。 加上returnImageFromFlicrk方法中的警告

[imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@“placeholder.png”]];

说在这个块中强烈捕获'imageView'可能会导致一个保留周期

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

   /*for (int i = 0; i < self.urlArray.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];


        [self returnImageFromFlickr:[self.urlArray objectAtIndex:i]];

        UIImageView *imageView = self.largePhoto;

        [subview addSubview:imageView];
        //subview.backgroundColor = [colors objectAtIndex:i];
        [self.scrollView addSubview:subview];

    }*/

    for (int i = 0; i < self.urlArray.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;
        UIImageView *imageView = [[UIImageView alloc] init];
        [self returnImageFromFlickr:[self.urlArray objectAtIndex:i] imageview:imageView];

        [self.scrollView addSubview:imageView];

    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * self.urlArray.count, self.scrollView.frame.size.height);
}

-(void)returnImageFromFlickr:(NSURL *)url imageview:(UIImageView *)imageView {
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    [imageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"placeholder.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        [imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

    }];


}

您没有为滚动内容平移设置正确的参数,这就是为什么您想要的所有图像都不在scrollView中显示的原因。 尝试更新ScrollView的内容平移以适应所有图像的宽度。 喜欢

self.scrollView.contentSize = CGSizeMake(Width,Height);

在这种情况下,高度将是单幅图像的高度,宽度将是(单幅图像的宽度*图像的总数)


你覆盖self.largePhoto,你需要在returnImageFromFlickr中设置你的imageView

尝试这个

UIImageView *imageView = [[UIImageView alloc] init];
[self returnImageFromFlickr:[self.urlArray objectAtIndex:i] imageview:imageView];

-(void)returnImageFromFlickr:(NSURL *)url imageview:(UIImageView *)imageView {
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    [imageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"placeholder.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        [imageView setImage:image];

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

}];

}


我需要做的就是将代码从viewDidLoad移动到viewDidApear

链接地址: http://www.djcxy.com/p/74379.html

上一篇: ios. ScrollView only showing one subview with image

下一篇: UITableViewCell, show delete button on swipe