IOS learning notes-download network resources (download images)

Source: Internet
Author: User

Download the image when you connect to the network by parsing the obtained URL address.

First, create a. plist file that contains the image URL ,:


Create a LinCell class that inherits from UITableViewCell and is used to draw the row information of the table view. The activity icon is used to record whether the image has been downloaded. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + INTaLmjOxLz + wO + 0 + sLro7o8L3A + fill = "brush: java;"> @ interface LinCell: UITableViewCell // create activity flag object @ property (retain, nonatomic) UIActivityIndicatorView * pActivityView; @ endCode in the. m file:

@ Implementation LinCell // release the created object-(void) dealloc {[_ pActivityView release]; [super dealloc];}-(id) initWithStyle :( UITableViewCellStyle) style reuseIdentifier :( NSString *) reuseIdentifier {self = [super initWithStyle: style reuseIdentifier: reuseIdentifier]; if (self) {// initialization activity flag self. pActivityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray]; // set the range of the activity flag [self. pActivityView setFrame: CGRectMake (10, 10, 40, 40)]; // Add the activity flag to the current view [self addSubview: self. pActivityView];} return self;}-(void) setSelected :( BOOL) selected animated :( BOOL) animated {[super setSelected: selected animated: animated];} @ end
In the View Controller, you must follow the UITableViewDataSource protocol and UITableViewDelegate protocol. The download process puts each download event in the thread pool, which is managed by the system.

Code in the. h file:

@ Interface LinViewController: UIViewController
 
  
// Create a table view object @ property (retain, nonatomic) UITableView * mTableView; // create an array object @ property (retain, nonatomic) NSArray * mArray; // create a thread pool object @ property (retain, nonatomic) NSOperationQueue * mQueue; @ end
 
Code in the. m file: (import the LinCell class before necessary)

@ Implementation LinViewController // release the created object-(void) dealloc {[_ mTableView release]; [_ mArray release]; [_ mQueue release]; [super dealloc];} -(void) viewDidLoad {[super viewDidLoad]; // initialize the table view self. mTableView = [[UITableView alloc] initWithFrame: self. view. frame style: UITableViewStylePlain]; // sets the delegate object self of the table view. mTableView. dataSource = self; self. mTableView. delegate = self; // Add the table View to the current view. view addSubview: self. mTableView]; // get. path of the plist file NSString * pPath = [[NSBundle mainBundle] pathForResource: @ "myData" ofType: @ "plist"]; // initialize the array and store it. plist File Content self. mArray = [[NSArray alloc] initWithContentsOfFile: pPath]; // initialize the thread pool self. mQueue = [[NSOperationQueue alloc] init] ;}# pragma mark --- UITableViewDataSource // obtain the number of rows in the table view-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return [self. mArray count];} // render each row of the table view-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// set a static string, mark static NSString * identifer = @ "identifer"; // obtain the cell content in the cache LinCell * pCell = (LinCell *) [tableView dequeueReusableCellWithIdentifier: identifer]; // if it is null, create a new cell if (nil = pCell) {pCell = [[LinCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: identifer];} // set the activity flag to start running, indicating that the progress starts [pCell. pActivityView startAnimating]; // create a sub-thread NSInvocationOperation * pInvocation = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (downLoadImage :) object: indexPath]; // put the thread into the thread pool for management [self. mQueue addOperation: pInvocation]; return pCell;} // Method for downLoadImage. The method must be-(void) downLoadImage :( NSIndexPath *) when the network is connected *) path {// obtain the number of rows in the table view NSInteger row = [path row]; // obtain the NSString * pString = [self. mArray objectAtIndex: row]; // convert to RUL address NSURL * pURL = [NSURL URLWithString: pString]; // convert network resources to NSData type NSData * pData = [[NSData alloc] initWithContentsOfURL: pURL]; // obtain the network image UIImage * pImage = [UIImage imageWithData: pData]; // find the current line LinCell * pCell = (LinCell *) [self. mTableView cellForRowAtIndexPath: path]; // you can specify pCell for a cell image. imageView. image = pImage; // set the activity flag to stop [pCell. pActivityView stopAnimating]; // remove the activity flag [pCell. pActivityView removeFromSuperview];} # pragma mark ------- UITableViewDelegate // obtain the height of each row in the table view-(float) tableView :( UITableView *) tableView rows :( NSIndexPath *) indexPath {return 60 ;} -(void) didReceiveMemoryWarning {[super didreceivemorywarning];} @ end




Related Article

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.