本文介绍了objective - C:从URL加载图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起问题标题。我找不到合适的标题。

Sorry for question title. I can not find a suitable title.

当我打开<$ c时,我有来自url的 UITableView 内容图片$ c> UITableView 在加载图像并且花费时间之前,View才显示。

I have UITableView content images from url when i open the UITableView the View did not show until the images loaded and that takes along time.

我通过php从JSON获取图像。

I get the images from JSON by php.

我想显示表格,然后显示图片加载过程。

I want to show the table and then images loading process.

这是我的应用程序中的代码:

This is code from my app:

NSDictionary *info = [json objectAtIndex:indexPath.row];
cell.lbl.text = [info objectForKey:@"title"];
NSString *imageUrl = [info objectForKey:@"image"];
cell.img.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]];
[cell.img.layer setBorderColor: [[UIColor blackColor] CGColor]];
[cell.img.layer setBorderWidth: 1.0];

return cell;

抱歉我的英语很差。

推荐答案

在单独的线程上执行Web请求,以阻止UI。以下是使用 NSOperation 的示例。请记住,只有在主线程更新UI,如图与 performSelectorOnMainThread:

Perform the web request on a separate thread, to not block the UI. Here is an example using NSOperation. Remember to only update the UI on the main thread, as shown with performSelectorOnMainThread:.

- (void)loadImage:(NSURL *)imageURL
{
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                        initWithTarget:self
                                        selector:@selector(requestRemoteImage:)
                                        object:imageURL];
    [queue addOperation:operation];
}

- (void)requestRemoteImage:(NSURL *)imageURL
{
    NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
    UIImage *image = [[UIImage alloc] initWithData:imageData];

    [self performSelectorOnMainThread:@selector(placeImageInUI:) withObject:image waitUntilDone:YES];
}

- (void)placeImageInUI:(UIImage *)image
{
    [_image setImage:image];
}

这篇关于objective - C:从URL加载图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 10:04