使用sdwebimage在tableview中的动态图像高度 [英] Dynamic image height in tableview with using sdwebimage
问题描述
我想在表格视图中显示动态高度图像,图像来自url,所以我正在使用sdwebimage。
Sdwebimage在后台线程中设置图像。
I want dynamic height image show in tableview, image is come from url so I am using the sdwebimage. Sdwebimage set image in background thread.
[self.imageView sd_setImageWithURL:[NSURL URLWithString: encodedurl1] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
我正在此线程中更新图像视图的约束高度。
I am update the constraint height of image view in this thread.
[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];
}];
此代码对我有用,但是我在后台线程
中获得了图像大小花一点时间更新约束图像视图的高度
This code is helpful for me but I get image size in background thread So, I take a time to update the height of constraint image view
我正在搜索并仔细检查了最近两天。
这种类型的问题吸引了很多开发人员,但在堆栈溢出的任何问题中都没有答案。
I am search and goggling last 2 days. This type issue get many developer, but not answer in any question in stack overflow.
推荐答案
尝试使用此Helper方法
Try this Helper method which created for my project.
-(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
In viewDidLoad
tblView.estimatedRowHeight = 44.0 ;
tblView.rowHeight = UITableViewAutomaticDimension;
还要给imageview赋予top,bottom,lead,trailing约束和imageview大于等高约束
And also Give top,bottom,leading,trailing constraints to imageview and greater than equal height constraint to imageview
这篇关于使用sdwebimage在tableview中的动态图像高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!