iOS Delay loading network pictures

Source: Internet
Author: User

Downloading pictures on the Internet is very slow, in order to not affect the experience, choose to load the image delay is a good way. A tableview list, left temporarily without a diagram

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (nsindexpath*) Indexpath

{

static NSString *cellidentifier = @ "MyCell";

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];

if (cell = = nil)

{

cell = [[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle

Reuseidentifier:cellidentifier] autorelease];

Cell.selectionstyle = Uitableviewcellselectionstylenone;

}

Set up cell some data

Apprecord *apprecord = [Self.entries objectAtIndex:indexPath.row];

Cell.textLabel.text = Apprecord.appname;

Cell.detailTextLabel.text = apprecord.artist;

If no picture exists

if (!apprecord.appicon)

{

if (self.tableView.dragging = = no && self.tableView.decelerating = = no)//not in drag and deceleration, start downloading pictures

{

[Self Starticondownload:apprecord forindexpath:indexpath];

}

Set picture as a blank picture (Waiting for download)

Cell.imageView.image = [UIImage imagenamed:@ "Placeholder.png"];

}

If you have a picture

Else

{

Cell.imageView.image = Apprecord.appicon;

}

return cell;

}

 The point is[Self Starticondownload:apprecord forindexpath:indexpath];

-(void) Starticondownload: (Apprecord *) Apprecord Forindexpath: (Nsindexpath *) Indexpath

{

Icondownloader *icondownloader = [imagedownloadsinprogress Objectforkey:indexpath];

if (Icondownloader = = nil)//is already in the download does not have to repeat the download, did not go down in the download

{

Icondownloader = [[Icondownloader alloc] init];

Icondownloader.apprecord = Apprecord;

Icondownloader.indexpathintableview = Indexpath;

Icondownloader.delegate = self;

[Imagedownloadsinprogress Setobject:icondownloader Forkey:indexpath];

[Icondownloader Startdownload];

[Icondownloader release];

}

}


Icondownloader is a download picture encapsulation class Key method: Icondownloader.delegate = self; [Icondownloader Startdownload]; One is a delegate, in the future tell the self download to complete the update picture one is their own method to start the network download picture delegate call method, reset the picture

-(void) Appimagedidload: (Nsindexpath *) Indexpath

{

Icondownloader *icondownloader = [imagedownloadsinprogress Objectforkey:indexpath];

if (icondownloader! = nil)

{

UITableViewCell *cell = [Self.tableViewcellForRowAtIndexPath:iconDownloader.indexPathInTableView];

Cell.imageView.image = IconDownloader.appRecord.appIcon;

}

}

Methods in the class Icondownloader

-(void) startdownload

{

Self.activedownload = [Nsmutabledata data];

Nsurlconnection *conn = [[Nsurlconnection alloc] Initwithrequest:

[Nsurlrequest Requestwithurl:

[Nsurl URLWithString:appRecord.imageURLString]] Delegate:self];

Self.imageconnection = conn;

[Conn release];

}

Last Nsurlconnectionthe delegate needs to be implemented by itself.

iOS Delay loading network pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.