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;