如何将图像下载传输到后台线程而不是主线程?

时间:2014-07-23 00:08:24

标签: ios objective-c json uiimageview

我正在构建一个应用程序,其中我将图像从互联网(特定的JSON端点)下载到我的tableview的自定义单元格中。一切都很好,除了我运行应用程序时从JSON字符串中提取的图像加载速度非常慢的事实。有谁知道我怎么能让事情变得更快?这是我的代码:

DoctorsViewController.h

#import <UIKit/UIKit.h>

@interface DoctorsViewController : UIViewController {

    IBOutlet UITableView *DoctorsTableView;

    NSArray *Doctors;
    NSMutableData *data;

}

DoctorsViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    NSURL *url = [NSURL URLWithString:@"URL HERE"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    Doctors = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
    [DoctorsTableView reloadData];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure that you're connected to 3G or Wi-Fi." delegate:nil
                                              cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [errorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (int)numberOfSectionsInTableView: (UITableView *)tableview
{
    return 1;
}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [Doctors count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *DoctorsTableIdentifier = @"DoctorsCell";

    DoctorsCell *cell = (DoctorsCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
    if (cell == nil) 
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DoctorsCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    } 

    cell.firstnameLabel.text = [[Doctors objectAtIndex:indexPath.row] objectForKey:@"node_title"];

    cell.descriptionLabel.text = [[Doctors objectAtIndex:indexPath.row] objectForKey:@"Opening Paragraph"];

    NSString *firstLink = [[NSString alloc] init];

    firstLink = [[[Doctors objectAtIndex:indexPath.row] objectForKey:@"Image"] objectForKey:@"filename"];

    NSString *secondLink = [[NSString alloc] init];

    secondLink = [NSString stringWithFormat:@"/sites/default/files/field/image/%@",firstLink];
      NSLog(@"second link is %@", secondLink);

    cell.featureImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:secondLink]]];

    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 373;
}

0 个答案:

没有答案
相关问题