![](/img/trans.png)
[英]UIImageView+AFNetworking Success Block with resize image and return image height to resize cell height
[英]Resize the Table cell height as per downloaded image height
我在運行時得到圖像的 URL,我想下載並在表格中顯示這些圖像。 圖像將被異步下載。 更重要的是,我想以實際尺寸顯示所有這些圖像。
請幫助我。 提前致謝:)
在委托方法上,您必須在完成下載后更新圖像,您可以使用
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]
withRowAnimation:UITableViewRowAnimationAutomatic];
這將再次調用
-(CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
因此,您應該使用圖像的高度(如果存在)、默認高度或占位符圖像(如果有)。
首先,我強烈建議您使用SDWebImage進行異步下載,如果您還沒有這樣做的話。 根據您的需要,您可以讓它在 memory 或磁盤上緩存圖像 - 后者以正確的方式完成,將它們保存在特殊的緩存文件夾中,因此 iOS 可以在空間不足時刪除它們。 它還在 UIImageView 上提供了一個方便的類別,類似於 AFNetworking 為您提供的類別,但具有更多選項和一個可選的完成塊。 這樣你就可以做這樣的事情:
#import <SDWebImage/UIImageView+WebCache.h>
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create the cell...
[cell.myImageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
if (image != nil) {
[self.images insertObject:image atIndex:indexPath.row];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];
}
}];
return cell;
}
這樣你就可以異步下載圖像,正確設置 UIImageView,但你也可以使用塊將圖像保存在 NSMutableArray 中,這樣你就可以告訴 tableview 正確的高度。 它還告訴表視圖更新其圖像已加載的單元格的高度。 然后當表格視圖需要知道給定單元格的高度時,如果圖像已加載,我們將從中獲取尺寸:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
id image = [self.images objectAtIndex:indexPath.row];
if ([image isKindOfClass:[NSNull class]]) {
return defaultHeight;
} else {
return [image size].height + topPadding + bottomPadding;
}
}
這樣我們甚至可以知道屏幕外圖像的高度(例如,當您滾動到底部時)。 此解決方案假定您首先使用 NSNull 值填充 NSMutableArray,然后在加載圖像時將其替換為圖像。 最后,如果您願意,甚至可以在顯示單元格之前使用 SDWebImageManager 手動開始下載。
您可以根據下載的圖像制作您的UIImageView
大小,但它看起來會很糟糕。
所以我建議你把所有的圖片都做成相同的大小,這樣看起來會很好看
您可以使用它來制作相同大小的所有圖像。
+ (UIImage *)imageScaledToSizeWithImage:(UIImage *)imagewww andsizeee:(CGSize)size
{
//avoid redundant drawing
if (CGSizeEqualToSize(imagewww.size, size))
{
return imagewww;
}
//create drawing context
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
//draw
[imagewww drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)];
//capture resultant image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//return image
return image;
}
但是,如果您真的想顯示具有不同行大小的表,請在運行時更改行大小
當您獲得圖像時,然后將圖像保存在字典中,鍵值為行的indexPath。
[tableImageDict setObject:image forKey:[NSString stringWithFormat:@"%i,%i",indexPath.row,indexPath.section]];
然后重新加載您的表格行。
[table reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationNone];
它會改變你的行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *image = (UIImage *)[tableImageDict objectForKey:
[NSString stringWithFormat:@"%i,%i",
indexPath.row,indexPath.section]];
if (image != nil)
{
return image.size.height;
}
else{
return 44.0;
}
}
查看heightForRowAtIndexPath:
。 這是關於它的另一篇文章。
為此你必須創建自定義 UITableviewCell 並且你可以使用此代碼設置表格視圖單元格的大小
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowHeight =0.0f;
// here u can get particular Url by indexPath.row, now create UIImage object using that Url
CGRect bioHeadingFrame = image.frame;
rowHeight = size.height; //get the height of that image
return rowHeight;
}
現在你的表格單元格的高度根據圖像高度增加
如果異步下載是您真正的問題,請參閱AFNetworking庫的 UIImageView 類別。 特別是這個 function:
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage
您可以將此 UIimageview 類別包含到您的項目中,並設置從該 class 派生的單元格圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.