【问题标题】:Only last image is loading from url in scrollview只有最后一个图像是从滚动视图中的 url 加载的
【发布时间】:2018-04-21 08:59:00
【问题描述】:

在我的 iOS 应用程序中,我有很多视图,图像和标签是的子视图。看法。我的问题是从 url 或远程服务器加载图像时,只有最后一个图像是从 url 加载的。其他图像使用占位符加载。尝试了很多方法,但无法解决。下面提供了我的代码和屏幕截图

catScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 10, self.view.frame.size.width,300)];
catScrollView.contentSize = CGSizeMake(([popularCategoryArray count] * 90)/1.8 + 10, catScrollView.frame.size.height);
[_parentScrollView addSubview:catScrollView];
catScrollView.backgroundColor=UIColorFromRGB(0xE8F7FE);
catScrollView.userInteractionEnabled=YES;
catScrollView.scrollEnabled=YES;
catScrollView.delegate=self;


for(int i =0;i<[popularCategoryArray count];i++)
{
    Category* category=[popularCategoryArray objectAtIndex:i];
     UIView *catView = [[UIView alloc] init];

    if(i>=[popularCategoryArray count]/2+1)
    {

        catView.frame = CGRectMake(10 + y * 100, 4*10+90+10, 90, 120);



        y++;
    }
    else
    {
        catView.frame = CGRectMake(10 + x * 100, 10, 90, 120);

        x=i;


    }
    imageCat=[[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 70, 90)];
    imageCat.backgroundColor=[UIColor clearColor];

    [imageCat sd_setImageWithURL:[NSURL URLWithString:category.ImageURL]
                placeholderImage:[UIImage imageNamed:@"wish_list"]];


    [catView addSubview:imageCat];




    UILabel *labelCat=[[UILabel alloc]initWithFrame:CGRectMake(0, 95, catView.frame.size.width, 25)];
    labelCat.backgroundColor=[UIColor clearColor];
    labelCat.text=category.BngTitle;
    labelCat.textAlignment=NSTextAlignmentCenter;
    labelCat.font=[UIFont fontWithName:@"HelveticaNeue-Thin" size:12.0];
    [catView addSubview:labelCat];


    catView.backgroundColor=[UIColor clearColor];



    [catScrollView addSubview:catView];



}

截图

【问题讨论】:

  • Category 这是你的数组吗?
  • 是的,类别的文本和图像 url 正在从数组中加载
  • 您能分享该数组中包含图像的索引吗?或者请分享一个结果。
  • 您的代码看起来正确,但我们不知道循环前后的imageCat 变量发生了什么。尝试将其作为UIImageView *imageCat = ... 中的局部变量。还打印出带有NSLog("%@", category.ImageURL) 的网址,并检查它们是否正确且功能正常。
  • 为什么imageCat 是全球性的?它应该是局部变量。

标签: objective-c uiscrollview uiimageview sdwebimage uiscrollviewdelegate


【解决方案1】:

发生的情况是,在UIView+Webcache.msd_internalSetImageWithURL 方法中,每次运行循环都会执行以下代码。

NSString *validOperationKey = operationKey ?: NSStringFromClass([self class]);
[self sd_cancelImageLoadOperationWithKey:validOperationKey];

operationKey 始终相同,因为在您的默认调用中它没有设置,所以它被设置为 nil,当它为 nil 时,它只是类名。 SDWebImage 有一个非常激进的政策,以这种方式取消所有以前的请求。

有些人认为它只对同一 URL 的多个请求执行此操作,但此代码不会因 URL 而异,因此这可能是一个错误或不是。您最好的选择可能是调用sd_internalSetImageWithURL 方法并设置您自己的operationKey(其中包含一个数字,以便它们都不同)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多