2017-08-03 29 views
0

我想要在tableview中顯示動態高度圖像,圖像來自url,所以我使用sdwebimage。 Sdwebimage在後臺線程中設置圖像。使用sdwebimage在tableview中動態圖像高度

[self.imageView sd_setImageWithURL:[NSURL URLWithString: encodedurl1] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { 
    }]; 

我正在更新此線程中圖像視圖的約束高度。

[self.imageView sd_setImageWithURL:[NSURL URLWithString: encodedurl1] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { 
    CGFloat multiplier = (CGRectGetWidth(self.imageView.bounds)/image.size.width); 
    [self.imageViewHeightConstraint setConstant:multiplier * image.size.height]; 
}]; 

此代碼對我來說是有益的,但我得到的圖像大小在後臺線程 所以,我需要時間來更新約束圖像視圖

我搜索和goggling最後2天的高度。 這種類型的問題得到了許多開發人員,但在堆棧溢出中沒有回答任何問題。

+0

嘗試在下載圖像時重新加載索引路徑。併爲imageview提供top,bottom,leading,trailing約束,併爲imageview提供大於等於height height的約束。並將其添加到viewDidLoad中:tblViewDetail.estimatedRowHeight = 44.0; tblViewDetail.rowHeight = UITableViewAutomaticDimension; – KKRocks

+0

嘗試僅爲tablesView傳遞estimatedRowHeight。 –

回答

0

在主線程中更新嘗試這爲我的項目創造了這個助手方法。

-(void)imageWithURL:(NSURL *)imageurl WithIndexPath:(NSIndexPath *)indexpath WithCallback:(void(^)(UIImage *downloadedImage,BOOL isCache , NSIndexPath *imageIndexPath))callback{ 
    if (![[SDWebImageManager sharedManager ]diskImageExistsForURL:imageurl]) { 
     [[SDWebImageManager sharedManager]downloadImageWithURL:imageurl options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { 
      // resize image here 
      callback(image , NO,indexpath); 
     }]; 

    } 
    else{ 
     UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:[imageurl absoluteString]] ; 
     // resize image here 
     callback(image, YES ,indexpath); 
    } 

} 

調用此方法的cellForRowAtIndexPath

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
        [self imageWithURL:[NSURL URLWithString:objPreparations.strImageURL] WithIndexPath:indexPath WithCallback:^(UIImage *downloadedImage, BOOL isCache, NSIndexPath *imageIndexPath) { 

         if (downloadedImage) { 
          dispatch_async(dispatch_get_main_queue(), ^{ 
           UITableViewCell *existcell = [tableView cellForRowAtIndexPath:imageIndexPath]; 
           if (existcell) { 
            // assign image to cell here 
            [tableView reloadRowsAtIndexPaths:@[imageIndexPath] withRowAnimation:UITableViewRowAnimationNone]; 
           } 
          }); 
         } 
        }]; 
       }); 

不要忘記

在viewDidLoad中

tblView.estimatedRowHeight = 44.0 ; 
tblView.rowHeight = UITableViewAutomaticDimension; 

,也給頂部,底部,領先,落後合作對圖像視圖的限制和對圖像視圖的高度限制大於等於

+0

這是需要很多時間的稱重傳感器,因爲在行重新加載。 我在桌上有很多排,我的應用程序有很多圖像像fb。 –

+0

你試過這個嗎? – KKRocks

+0

,因爲這種方法檢查已經下載的圖像,那麼它將附帶該圖像。但如果你用你的方法運行,那麼它會每次都下載圖像。 – KKRocks

0

不喜歡

[self.imageView sd_setImageWithURL:[NSURL URLWithString: encodedurl1] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     CGFloat multiplier = (CGRectGetWidth(self.imageView.bounds)/image.size.width); 
     [self.imageViewHeightConstraint setConstant:multiplier * image.size.height]; 
     [self setNeedsUpdateConstraints]; 
    }); 
}]; 
+0

在主線程中,不更新圖像約束的高度,因爲在載入圖像之前更新高度。 –

+0

嘗試添加'[self setNeedsUpdateConstraints]',更新了答案。 – Akhilrajtr

+0

爲動態高度添加所有代碼 - 謝謝 –