Only last image is loading from url in scrollview

2019-07-26 14:10发布

问题:

In my iOS app I have have a lots of view and image and labels are the subviews of the. view. My problem is while loading images from url or remote server only last image is loading from url . Other images are loaded with placeholders. Tried many ways but unable to solve it . My code and screenshot are provided below

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



}

Screenshot

回答1:

What is happening is that in UIView+Webcache.m's sd_internalSetImageWithURL method, the following code is executed on every run through your loop.

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

The operationKey is always the same because in your default invocation it is not set, so it's possed as nil, and when it's nill it's just the class name. SDWebImage has a very aggressive policy of cancelling all previous requests that way.

Some people believe it only does that for multiple requests of the same URL, but this code does not vary on URL so that may be a bug or not. Your best bet may be to call the sd_internalSetImageWithURL method and set your own operationKey (something with a number in it so that they're all different).