iOS UIImageView自适应cell高度

作者: 殇鑫 | 来源:发表于2017-05-19 16:45 被阅读0次

前言

整理代码时发现以前攻克的一个难题: 如何更好的展示图片。公司是做有关教育的项目的。用户可以随意剪切图片,并且要展示原图。这样我们客户端就不是很好的展示。后来就在自定义cell上想了各种办法。遇到这种问题你会怎么去做呢?如果有更好的方法和建议请告知。。。

核心代码展示

  __block CGFloat topicViewHeight = .0f;
    
    NSArray *imageUrls = [model.imageUrl componentsSeparatedByString:@","];
    
    NSLog(@"%@",imageUrls);
    
    for (int i = 0 ; i < imageUrls.count ; i++) {
        
        UIImageView *topicImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, SWIDTH - 20, 150)];
        
        @autoreleasepool {
            [topicImgView sd_setImageWithURL:[NSURL URLWithString:imageUrls[i]]
                            placeholderImage:[UIImage imageNamed:@"topic_loading.png"]
                                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL){
                                       
                                       if (image == nil) {
                                           image = [UIImage imageNamed:@"topic_faile"];
                                       }
                                       topicImgView.image = image;
                                       //显示高度
                                       CGFloat height =.0f;
                                       height = image.size.height * (SWIDTH - 20) / image.size.width;
                                       
                                       topicImgView.frame = CGRectMake(10, topicViewHeight +5, SWIDTH - 20, height);
                                       
                                     topicViewHeight += height;
                                       
                                       model.cellHeight = topicViewHeight+20;
                                       
                                       if (self.changeCellHeight) {
                                           self.changeCellHeight();
                                       }
                                       
                                   }];
            
            [_backView addSubview:topicImgView];
            _backView.frame = CGRectMake(0, 20, SWIDTH, topicViewHeight);
        }     
    }

Demo链接

https://github.com/DearWang/LoadImage
http://code.cocoachina.com/view/135097

相关文章

网友评论

    本文标题:iOS UIImageView自适应cell高度

    本文链接:https://www.haomeiwen.com/subject/jzbyxxtx.html