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

时间:2013-11-09 04:15:37

标签: ios objective-c uiimageview lazy-loading

当应用程序启动时,我将UIImageViews添加到没有图像的屏幕。然后我使用NSThread逐个加载图像并为这些视图设置它。在加载所有图像并完成线程之前,UiImageViews保持空白。为什么在我为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 < 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];
}

3 个答案:

答案 0 :(得分:0)

一般来说,UIKit不是线程安全的。从后台线程中的URL获取数据并在主线程中设置图像。

编辑: 在主线程中执行类似

的操作
dispatch_async(someDispatchQ, ^{
    [self functionToGetData];
});

并在functionToGetData内执行

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

这将确保您使用后台线程来获取数据/生成图像并使用主线程来设置图像。这也消除了使用计时器不断轮询后台线程的需要,因为后台线程一接收到图像就会自动将图像设置发送到主线程。

答案 1 :(得分:0)

使用 - 导入类ImageDownloader.h&amp; .M
链路&GT; https://github.com/psychs/imagestore/tree/master/Classes/Libraries/ImageStore

//现在使用此方法下载

-(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;
    }
}

答案 2 :(得分:0)

一个简单的解决方法是

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