Asynchronous loading of iOS tableView image Cache

Source: Internet
Author: User

Asynchronous loading of iOS tableView image Cache
1. create a viewController .. the hfile implements UIScrollViewDelegate and UITableViewDelegate, and declares ICTableViewDelegate (this is used to load the image if the image is cached, and request the image if there is no cache, cache it, and load it again ). the H file is as follows:

# Define KimageKey @ "photoFileUrl" // name of the key that stores the image URL in each item in the array # define KidKey @ "activityId" // the id of each item in the array is used cache # import
 
  
@ Protocol ICTableViewDelegate @ required-(void) cellImageDidLoad :( NSIndexPath *) indexPath image :( NSMutableArray *) imageArray; @ end @ interface ICTableViewController: UIViewController
  
   
{@ Public id
   
    
WQTableVieDelegate; NSMutableArray * tableDataArray; UITableView * wqTable;} @ end
   
  
 

The. m file is as follows:
-(Void) loadCellImage {// method to load an image when the image is cached. If no cache is available, request the image and cache it before loading NSArray * indexPathsForLoad = [wqTable indexPathsForVisibleRows]; for (NSIndexPath * item in indexPathsForLoad) {NSInteger rowNumberForCell = item. row; NSLog (@ "% li", (long) rowNumberForCell); NSLog (@ "% li", (unsigned long) [tableDataArray count]); if (rowNumberForCell> [tableDataArray count]-1) {return;} NSString * imageStr = tableDataArray [rowNu MberForCell] [@ "photoFileUrl"]; NSLog (@ "% @", imageStr); NSMutableArray * imageArray = [NSMutableArray array]; if ([imageStr length]! = 0) {NSArray * photoUrl = [imageStr componentsSeparatedByString: MULTI_FILES_SEPARATOR]; for (int I = 0; I
 
  
Then the child class inherits the parent class and implements the ICTableViewDelegate proxy method.
  
# Pragma mark ICTableViewDelegate-(void) initialize :( NSIndexPath *) indexPath image :( NSMutableArray *) imageArray {EventShowTableViewCell * cell = (EventShowTableViewCell *) [_ eventListTableView cellForRowAtIndexPath: indexPath]; if ([imageArray count]! = 0) {for (int I = 0; I
   
    
CustomPhotoBtn: A control for loading image encapsulation. UIImage * thumbImg = [FileTransferHelp thumbnailWithImage :( UIImage *) imageArray [I] size: CGSizeMake (60, 40)]; [photoBtn. imgView setImage: thumbImg]; [cell. contentView addSubview: photoBtn];} else {CustomPhotoBtn * photoBtn = (CustomPhotoBtn *) [cell. contentView viewWithTag :( I + 1) * 10]; UIImage * thumbImg = [FileTransferHelp thumbnailWithImage :( UIImage *) imageArray [I] size: CGSizeMake (60, 40)]; [photoBtn. imgView setImage: thumbImg]; [cell addSubview: photoBtn] ;}}}
   
Set the content of each cell in the subclass

-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath

Write down
CustomPhotoBtn * photoBtn = [CustomPhotoBtn customPhotoBtn]; // loads a control of image encapsulation [photoBtn. fileFullName setText: url]; NSString * imageName = [url stringByAppendingString: [NSString stringWithFormat :@". temp "]; NSLog (@" imageName % @ ", imageName); NSString * imageDataPath = [NSHomeDirectory () stringByAppendingPathComponent: [@" Library/Caches/"stringByAppendingString: imageName] // obtain the image NSLog from the cache (@ "imageDataPath % @", imageDataPath); // [data writeToFile: imageDataPath atomically: YES]; UIImage * image = [UIImage imageWithData: [NSData dataWithContentsOfFile: imageDataPath]; UIImage * thumbImg = [FileTransferHelp placement: image size: CGSizeMake (60, 40)]; if (thumbImg) {[photoBtn. imgView setImage: thumbImg];} return photoBtn;



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.