IOS UITableView 加载未知宽高图片的解决方案
生活随笔
收集整理的這篇文章主要介紹了
IOS UITableView 加载未知宽高图片的解决方案
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在開發中遇到了UITableView列表 UITableViewCell裝載圖片但不知Image的寬高 問題。
在解決該問題的時候,首先想到的是異步加載圖片 采用第三方框架SDWebImage 實現對圖片異步下載和緩存
以下是我采用的方法幾個關鍵地方
1.計算UITableView的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {NSString *imgURL = self.electionPictureArray.count > indexPath.row ? self.electionPictureArray[indexPath.row] :nil;if (imgURL) {//根據當前Row的ImageUrl作為Key獲取圖片緩存UIImage *img = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey: imgURL ];if (!img) {img = [UIImage resizedImageWithName:@"childshow_placeholder"];;}CGFloat height = img.size.height *Main_Screen_Width/img.size.width;//Image寬度為屏幕寬度 ,計算寬高比求得對應的高度NSLog(@"----------------return Height:%f",height);return height;}return 0;}
2.在UITableViewCell中實現圖片的下載,回調下載完成刷新頁面代理
- (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;}在cell中的setImageUrl中進行下載圖片
-(void) setImageUrl:(NSString *)imageUrl{if (imageUrl) {_imageUrl = imageUrl;UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imageUrl];// 沒有緩存圖片if (!cachedImage) {__weak typeof(self) target = self;// 利用 SDWebImage 框架提供的功能下載圖片[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageDownloaderUseNSURLCache progress:^(NSInteger receivedSize, NSInteger expectedSize) {} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {// 保存圖片[[SDImageCache sharedImageCache] storeImage:image forKey:imageUrl toDisk:YES]; // 保存到磁盤if (imageUrl == target.imageUrl) {[target configPreviewImageViewWithImage:image];}if ([self.delegate respondsToSelector:@selector(reloadCellAtIndexPathWithUrl:)]) {[self.delegate reloadCellAtIndexPathWithUrl:imageUrl];}}];}else{[self configPreviewImageViewWithImage:cachedImage];}} } /*** 加載圖片成功后設置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.在Controller中實現代理方法,
-(void)reloadCellAtIndexPathWithUrl:(NSString *)url{if (url) {for (int i = 0; i< self.electionPictureArray.count; i++) {//遍歷當前數據源中并找到ImageUrl NSString *imgURL = self.electionPictureArray.count >i ? self.electionPictureArray[i] :nil;if ([imgURL isEqualToString:url]) {//獲取當前可見的Cell NSIndexPathsNSArray *paths = self.tableView.indexPathsForVisibleRows;
//判斷回調的NSIndexPath 是否在可見中如果存在則刷新頁面NSIndexPath *pathLoad = [NSIndexPath indexPathForItem:i inSection:0];for (NSIndexPath *path in paths) {if (path && path == pathLoad ) {[self.tableView reloadData];}}}}} }
?
IOS開發技術交流QQ群:491355147 歡迎加入
轉載于:https://www.cnblogs.com/fxiaoquan/p/5077041.html
總結
以上是生活随笔為你收集整理的IOS UITableView 加载未知宽高图片的解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JNI包装c++类
- 下一篇: 【CSS3】background-ori