只有最后一张图片是从滚动视图中的 url 加载的 [英] Only last image is loading from url in scrollview

查看:22
本文介绍了只有最后一张图片是从滚动视图中的 url 加载的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



}

截图

推荐答案

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

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

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

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.

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

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).

这篇关于只有最后一张图片是从滚动视图中的 url 加载的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆