Ios 如何将图像下载转移到后台线程而不是主线程?
我目前正在构建一个应用程序,在这个应用程序中,我从互联网下载图像,并将一个JSON端点指定到我的tableview的自定义单元格中。一切都很好,除了当我运行应用程序时,我从JSON字符串中提取的图像加载速度非常慢。有人知道我怎样才能让东西装载得更快吗?这是我的密码: DoctorsViewController.hIos 如何将图像下载转移到后台线程而不是主线程?,ios,objective-c,json,uiimageview,Ios,Objective C,Json,Uiimageview,我目前正在构建一个应用程序,在这个应用程序中,我从互联网下载图像,并将一个JSON端点指定到我的tableview的自定义单元格中。一切都很好,除了当我运行应用程序时,我从JSON字符串中提取的图像加载速度非常慢。有人知道我怎样才能让东西装载得更快吗?这是我的密码: DoctorsViewController.h 很可能图像太大,连接速度太慢。平均文件大小是多少,加载了多少图像,加载的连接是什么?在主线程上加载图像是不好的。请参阅苹果公司的LazyTableImages示例应用程序。@Amada
很可能图像太大,连接速度太慢。平均文件大小是多少,加载了多少图像,加载的连接是什么?在主线程上加载图像是不好的。请参阅苹果公司的LazyTableImages示例应用程序。@Amadan这些图像约为640x400像素,当用户继续向下滚动表格视图时,加载的单元格数量是无限的。也许我应该将单元格数量限制在10?640x400可以从几十个字节的gif单色格式扩展到超过1兆字节,例如未压缩的32位bmp,因此它不会真正提供任何信息。如果图像很大,您可以尝试压缩它们,缩小大小,缓存。。。而且,完全按照@rmaddy做says@AmadanSDWebImage在这种情况下是否工作良好?
#import <UIKit/UIKit.h>
@interface DoctorsViewController : UIViewController {
IBOutlet UITableView *DoctorsTableView;
NSArray *Doctors;
NSMutableData *data;
}
- (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;
}