0

当应用程序启动时,我将 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];
}
4

3 回答 3

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

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

于 2013-11-09T04:41:09.447 回答
0

使用-导入类ImageDownloader.h & .m
链接- > 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;
    }
}
于 2013-11-09T07:46:32.613 回答
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];
    }
}
于 2013-11-09T08:47:44.233 回答