2017-08-03 108 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

为动态高度添加所有代码 - 谢谢 –