IOS UITableView 載入未知寬高圖片的解決方案,

來源:互聯網
上載者:User

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 NSIndexPaths NSArray *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 歡迎加入

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.