Dynamic image height in tableview with using sdwebimage

笑着哭i 提交于 2019-12-11 07:03:33

问题


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.


回答1:


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);
    }

}

Call this method in 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];
                                }
                            });
                        }
                    }];
                });

Dont Forgot

In viewDidLoad

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

And also Give top,bottom,leading,trailing constraints to imageview and greater than equal height constraint to imageview




回答2:


do the update in main thread like

[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];
     });
 }];


来源:https://stackoverflow.com/questions/45483639/dynamic-image-height-in-tableview-with-using-sdwebimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!