IOS UITableView solution for loading images with unknown width and height,

Source: Internet
Author: User

IOS UITableView solution for loading images with unknown width and height,

During development, the UITableView list UITableViewCell loads images but does not know the width and height of the Image.

To solve this problem, we first think of Asynchronously loading images using a third-party framework SDWebImage to achieve asynchronous download and caching of images.

The following are the key points of my approach:

1. Calculate the UITableView height

-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {NSString * imgURL = self. electionPictureArray. count> indexPath. row? Self. electionPictureArray [indexPath. row]: nil; if (imgURL ){
// Obtain the image cache UIImage * img = [[SDImageCache nvidimagecache] imageFromDiskCacheForKey: imgURL] based on the ImageUrl of the current Row as the Key; if (! Img) {img = [UIImage resizedImageWithName: @ "childshow_placeholder"];} CGFloat height = img. size. height * Main_Screen_Width/img. size. width; // The Image width is the screen width. Calculate the width-to-Height ratio to obtain the corresponding height NSLog (@ "---------------- return height: % f", Height); return height;} return 0 ;}

2. Download the image in UITableViewCell and call back the download to complete refreshing the page proxy.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    ElectronicBookCell *cell = [ElectronicBookCell cellWithTableView:tableView];    cell.imageUrl = self.electionPictureArray.count > indexPath.row ? self.electionPictureArray[indexPath.row] :nil;    cell.selectionStyle = UITableViewCellSelectionStyleNone;    return cell;    }

Download images from setImageUrl in cell

-(Void) setImageUrl :( NSString *) imageUrl {if (imageUrl) {_ imageUrl = imageUrl; UIImage * cachedImage = [[SDImageCache nvidimagecache] imageFromDiskCacheForKey: imageUrl]; // No cached image if (! CachedImage) {_ weak typeof (self) target = self; // use the functions provided by the SDWebImage framework to download images [[SDWebImageDownloader sharedDownloader] downloadImageWithURL: [NSURL URLWithString: imageUrl] options: SDWebImageDownloaderUseNSURLCache progress: ^ (NSInteger receivedSize, NSInteger expectedSize) {} completed: ^ (UIImage * image, NSData * data, NSError * error, BOOL finished) {// Save the image [[SDImageCache sharedImageCache] storeImage: image forKey: imageUrl toDisk: YES]; // save it to the disk if (imageUrl = target. imageUrl) {[target configPreviewImageViewWithImage: image];} if ([self. delegate respondsToSelector: @ selector (reloadCellAtIndexPathWithUrl :)]) {[self. delegate reloadCellAtIndexPathWithUrl: imageUrl] ;}}] ;}else {[self configPreviewImageViewWithImage: cachedImage] ;}}
/*** After the image is loaded successfully, set the image's frame */-(void) configPreviewImageViewWithImage :( UIImage *) image {_ previewWidth = Main_Screen_Width; _ previewHeight = image. size. height * Main_Screen_Width/image. size. width; CGRect rect = _ previewImageView. frame; rect. size. width = _ previewWidth; // image. size. width; rect. size. height = _ previewHeight; _ previewImageView. frame = rect; _ previewImageView. image = image; [self resetLayoutByPreviewImageView];}

3. Implement modern management methods in the Controller,

-(Void) reloadCellAtIndexPathWithUrl :( NSString *) url {if (url) {for (int I = 0; I <self. electionPictureArray. count; I ++ ){
// Traverse the current data source and find the ImageUrl NSString * imgURL = self. electionPictureArray. count> I? Self. electionPictureArray [I]: nil; if ([imgURL isw.tostring: url]) {// obtain the currently visible Cell NSIndexPaths NSArray * paths = self. tableView. indexPathsForVisibleRows;
// Determine whether the callback NSIndexPath is visible. If yes, refresh the NSIndexPath * pathLoad = [NSIndexPath indexPathForItem: I inSection: 0]; for (NSIndexPath * path in paths) {if (path & path = pathLoad) {[self. tableView reloadData] ;}}}}

 

IOS development technology exchange QQ group: 491355147 welcome to join

 

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.