Ios 图像未显示在预加载的UIImageView上

Ios 图像未显示在预加载的UIImageView上,ios,objective-c,uiimageview,lazy-loading,Ios,Objective C,Uiimageview,Lazy Loading,当应用程序启动时,我将UIImageViews添加到屏幕上,但没有图像。然后我使用NSThread逐个加载图像,并为这些视图设置它。UIImageView保持空白,直到加载所有图像并完成线程。为什么在我为UIImageView设置图像时图像不立即显示,为什么它要等待NSThread结束 要添加图像视图,请执行以下操作: for(i = value; i < val; i++){ UIButton *newbutton = [UIButton buttonWithType:UIBut

当应用程序启动时,我将UIImageViews添加到屏幕上,但没有图像。然后我使用
NSThread
逐个加载图像,并为这些视图设置它。UIImageView保持空白,直到加载所有图像并完成线程。为什么在我为
UIImageView
设置图像时图像不立即显示,为什么它要等待
NSThread
结束

要添加图像视图,请执行以下操作:

for(i = value; i < val; i++){
    UIButton *newbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [newbutton setBackgroundColor:[UIColor whiteColor]];
    UIImageView *newback = [[UIImageView alloc] init];
    newback.contentMode = UIViewContentModeScaleAspectFit;
    [newback setFrame:CGRectMake(0, 0, 140, 140)];
    [newbutton addSubview:newback];
    height = MIN(300, newSize.height);
    [newbutton setFrame:CGRectMake(currentX, currentY, width, height)];
    newbutton.layer.cornerRadius = 10;
    newbutton.clipsToBounds = YES;
    [newbutton addTarget:self action:@selector(processButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
for(i=value;i
要在线程中添加图像,我调用一个函数,该函数执行以下操作:

for(i = value; i < val; i++){
    UIImage *newimage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL    URLWithString:[NSString stringWithFormat:@“http://test.com/a.jpg”, hash]]]];
    UIButton *newbutton = (UIButton*)[self.subviews objectAtIndex:i];
    [((UIImageView*)[newbutton.subviews objectAtIndex:0]) newimage];
}
for(i=value;i
一般来说,
UIKit
不是线程安全的。在后台线程中从URL获取数据,并在主线程中设置图像

编辑: 在主线程中,执行以下操作

dispatch_async(someDispatchQ, ^{
    [self functionToGetData];
});
和内部
functionToGetData
do

for(i = value; i < val; i++){
    NSData *data = //get Data
    UIImage *newImage = //make image from data
    dispatch_async(dispatch_get_main_queue(), ^{
        [imageView setImage:newImage];
    });
}
for(i=value;i
这将确保您使用后台线程获取数据/生成图像,并使用主线程设置图像。这还消除了使用计时器不断轮询后台线程的需要,因为一旦收到图像,后台线程就会自动将图像设置分派给主线程。

使用- 导入类ImageDownloader.h和.m
链接->

//现在使用此方法下载

-(void)downloadImageWithURL:(NSString *)url{


    if(self.downloder)return; //If already downloading please stay away

    url=[NSString stringWithFormat:@"%@%0.0fx%0.0f/%@/%@",IMAGE_ENDPOINT,self.imgViewExhibitors.frame.size.width,self.imgViewExhibitors.frame.size.height,@"fit",[ImagePath forURLString:url]];

    self.downloder=[[ImageDownloader alloc] init];
    self.downloder.delegate=self;
    self.downloder.indicator=self.indicator;
    [self.downloder downloadImageWithURL:url];
}

-(void)imageDownloaded:(UIImage *)image forIndexPath:(NSIndexPath *)indexPath{
    if(image){
        //Put the downloaded image in dictionary to update while scrolling
        //[self.speakerData setObject:image forKey:@"image"];
        self.imgViewExhibitors.image=image;
    }
}
一个简单的解决办法是

for(i = value; i < val; i++){
    UIImage *newimage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL    URLWithString:[NSString stringWithFormat:@“http://test.com/a.jpg”, hash]]]];
    UIButton *newbutton = (UIButton*)[self.subviews objectAtIndex:i];

    // this ensures that the UI update (setting the image) is done on the main thread.
    // This is important for the UI to update properly.
    dispatch_async(dispatch_get_main_queue(), ^{
         [((UIImageView*)[newbutton.subviews objectAtIndex:0]) setImage:newimage];
    }
}
for(i=value;i
不应
[((UIImageView*)[newbutton.subviews objectAtIndex:0])newimage];
[((UIImageView*)[newbutton.subviews objectAtIndex:0])setImage:newimage]
你不应该在后台线程上弄乱UIKit对象。在主线程上设置图像。Roshan这是我在复制时弄乱的setImage。Eric如果我在主线程上这样做,应用程序将处于挂起状态,直到加载完所有图像。如何在不进行交互的情况下加载一个图像并一次显示它悬而未决?