前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >底牌项目中设置论坛中各个模块头图的代码

底牌项目中设置论坛中各个模块头图的代码

作者头像
用户1451823
发布2018-09-13 15:42:40
5400
发布2018-09-13 15:42:40
举报
文章被收录于专栏:DannyHoo的专栏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1337783

// 获取所有图片的宽和高

for (int i = 0; i < imageArr.count; i ++) {

CGSize size = [UIImageView downloadImageSizeWithURL:[NSURL URLWithString:imageArri]];

if (size.width == 0.0 || size.height == 0.0) {

NSURL *url = [NSURL URLWithString:imageArri];

//因为这个方法在子线程(全局队列)中执行,所以不需要考虑死线程的问题

SDWebImageManager *manager = SDWebImageManager sharedManager;

            manager diskImageExistsForURL:url;

UIImage * image = [UIImage alloc init];

if (manager diskImageExistsForURL:url) {

                image = [manager imageCache imageFromDiskCacheForKey:url.absoluteString];

            }else{

NSData *data = NSData dataWithContentsOfURL:url;

                image = UIImage imageWithData:data;

            }

            size = image.size;

        }

NSString * width = NSString stringWithFormat:@"%f", size.width;

NSString * height = NSString stringWithFormat:@"%f", size.height;

        widthArr addObject:width;

        heightArr addObject:height;

    }

// 设置头视图的内容

  • (void)setDataModel:(PostDaraModel *)dataModel{

_dataModel = dataModel;

    [_iconView sd_setImageWithURL:NSURL URLWithString:_dataModel.face placeholderImage:UIImage imageNamed:@"touxiang_moren"];

_nameView.text = _dataModel.username;

_timeView.text = _dataModel.addtime;

_titleView.text = _dataModel.title;

_textView.text = _dataModel.content;

CGFloat faceX = Margin30 * IPHONE6_W_SCALE;

CGFloat faceY = 28 * 0.5 * IPHONE6_H_SCALE;

CGFloat faceW = 76 * 0.5 * IPHONE6_W_SCALE;

CGFloat faceH = faceW;

_iconView.frame = CGRectMake(faceX, faceY, faceW, faceH);

// 这个方法容易报错 <Error>: CGContextDrawImage: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

// 昵称

CGFloat nameX = CGRectGetMaxX(_iconView.frame) + Margin22 * IPHONE6_W_SCALE;

CGFloat nameY = 38 * 0.5 * IPHONE6_H_SCALE;

NSMutableDictionary * nameDic = NSMutableDictionary dictionary;

    nameDicNSFontAttributeName = Font15;

CGSize nameSize = _dataModel.username sizeWithAttributes:nameDic;

_nameView.frame = (CGRect){{nameX, nameY}, nameSize};

// 时间

CGFloat timeX = _nameView.frame.origin.x;

CGFloat timeY = CGRectGetMaxY(_nameView.frame) + Margin14 * IPHONE6_H_SCALE;

NSMutableDictionary * timeDic = NSMutableDictionary dictionary;

    timeDicNSFontAttributeName = Font11;

CGSize timeSize = _dataModel.addtime sizeWithAttributes:timeDic;

_timeView.frame = (CGRect){{timeX,timeY},timeSize};

// 标题

CGFloat titleX = Margin30 * IPHONE6_W_SCALE;

CGFloat titleY = CGRectGetMaxY(_iconView.frame) + Margin30 * IPHONE6_H_SCALE;

CGFloat titleW = WIDTH - titleX - 15 * IPHONE6_W_SCALE;

NSMutableDictionary * titleDic = NSMutableDictionary dictionary;

    titleDicNSFontAttributeName = Font18;

CGRect titleRect = _dataModel.title boundingRectWithSize:CGSizeMake(titleW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:titleDic context:nil;

_titleView.frame = (CGRect){{titleX, titleY}, titleRect.size};

// 简介

CGFloat contentsX = titleX;

CGFloat contentsY = CGRectGetMaxY(_titleView.frame) + 12 * IPHONE6_H_SCALE;

CGFloat contentsW = WIDTH - contentsX - 15 * IPHONE6_W_SCALE;

UIFont *textFont = Font16;

CGSize textSize = [_dataModel.content sizeWithFont:textFont

constrainedToSize:CGSizeMake(contentsW, MAXFLOAT)];;

_textView.frame = CGRectMake(contentsX, contentsY, textSize.width, textSize.height);

//    _textView.backgroundColor = UIColor greenColor;

_textView.font = Font16;

_textView.numberOfLines = 0;

// 调整行间距

NSMutableAttributedString *attributedString = [NSMutableAttributedString alloc initWithString:_dataModel.content];

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle alloc init];

    paragraphStyle setLineSpacing:6;

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, _dataModel.content length)];

_textView.attributedText = attributedString;

    _textView sizeToFit;

// 重新设置装图片的视图的位置

if (_dataModel.imgs) {    // 如果有图片

CGFloat photosY = CGRectGetMaxY(_textView.frame) + 11 * IPHONE6_H_SCALE;

CGFloat height = 0;

CGFloat allH = 0;

for ( int i = 0; i < _dataModel.imgs.count; i ++) {

CGFloat h;

CGFloat w;

NSLog(@"_heightArri===%@", _heightArri);

            w = [_widthArri floatValue] * IPHONE6_W_SCALE;

            h = [_heightArri floatValue] * IPHONE6_W_SCALE;

NSLog(@"%f", h);

CGFloat scale = 1.0;

if (w == 0.0 || h == 0.0) {   // 如果获取不到图片的大小

                w = WIDTH - 30 * IPHONE6_W_SCALE;

                h = w;

            }else{  // 能够获取图片大小

if (w > WIDTH - 30 * IPHONE6_W_SCALE) { // 图片宽度大于

                    scale = (WIDTH - 30 * IPHONE6_W_SCALE)/w;

                    h = h * scale;

                }else{  // 图片宽度小于

                    h = [_heightArri floatValue] * IPHONE6_W_SCALE;

                    w = [_widthArri floatValue] * IPHONE6_W_SCALE;

                }

            }

NSLog(@"%f", h);

            height = height + h + 8 * IPHONE6_H_SCALE;

            allH += h;

//            _picView.frame = CGRectMake(15 * IPHONE6_W_SCALE, photosY , WIDTH - 30 * IPHONE6_W_SCALE,  height +700);

NSLog(@" _picView.frame.size.height:%f", _picView.frame.size.height);

        }

NSLog(@"allH:%f", allH);

NSLog(@"height%f", height);

_picView.frame = CGRectMake(15 * IPHONE6_W_SCALE, photosY , WIDTH - 30 * IPHONE6_W_SCALE,  height);

    }

// 底部的分割线

CGFloat botLineY;

// 先获知picView的大小再去设置它的位置

//    self layoutSubviews;

if (_dataModel.imgs) {

//        NSLog(@"%@", _dataModel.imgs);

        botLineY = CGRectGetMaxY(_picView.frame) + 14 * IPHONE6_H_SCALE;

    } else{

        botLineY = CGRectGetMaxY(_textView.frame) + 14 * IPHONE6_H_SCALE;

    }

_bottomLine.frame = CGRectMake(0, botLineY, WIDTH, 0.5);

//    _bottomLine.backgroundColor = UIColor greenColor;

_viewHeight = CGRectGetMaxY(_bottomLine.frame);

// 可以再layoutSubviews方法中直接返回该视图的frame

NSLog(@"_viewHeight:%f", _viewHeight);

self.frame = CGRectMake(0, 0, WIDTH, _viewHeight);

_picView.widthArr = _widthArr;

_picView.heightArr = _heightArr;

_picView.picArr = _dataModel.imgs;

}

// 设置装头视图中所有图片的视图

  • (void)setPicArr:(NSArray *)picArr{

_picArr = picArr;

NSMutableArray * arr = NSMutableArray array;

if (picArr.count >= 9) {

for (int i = 0; i < 9; i ++) {

            [arr addObject:picArri];

        }

_picArr = (NSArray *)arr;

    }

NSUInteger counts = self.subviews.count;

CGFloat height = 0;

CGFloat allH = 0;

for (int i = 0; i < counts; i ++) {

UIImageView * imageView = self.subviewsi;

// 9张图片以内显示上传的所有图片

if (i < _picArr.count) {

            imageView.hidden = NO;

if (picArr.count > 0) {

CGFloat h;    // 图片的高度

CGFloat w; // 图片的宽度

//                CGSize size = [UIImageView downloadImageSizeWithURL:[NSURL URLWithString:_picArri]];

NSLog(@"_heightArri===%@", _heightArri);

                h = [_heightArri floatValue] * IPHONE6_W_SCALE;

                w = [_widthArri floatValue] * IPHONE6_W_SCALE;

NSLog(@"%f", h);

CGFloat scale = 1.0;

if (w == 0) {   // 如果获取不到图片的大小

                    w = WIDTH - 30 * IPHONE6_W_SCALE;

                    h = w;

                }else{  // 能够获取图片大小

if (w > WIDTH - 30 * IPHONE6_W_SCALE) { // 图片宽度大于

                        scale = (WIDTH - 30 * IPHONE6_W_SCALE)/w;

                        h = h * scale;

                    }else{  // 图片宽度小于

                        h = [_heightArri floatValue] * IPHONE6_W_SCALE;

                        w = [_widthArri floatValue] * IPHONE6_W_SCALE;

                    }

                }

                allH += h;

if (w >0 && w < WIDTH - 30 * IPHONE6_W_SCALE) { // 图片居中显示

                    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {

                        make.centerX.equalTo(self.mas_centerX);

                        make.top.equalTo(self.mas_top).offset(height);

                        make.width.equalTo(@(w));

                        make.height.equalTo(@(h));

                    }];

//                    imageView.frame = CGRectMake(self.center.x-w/2-10, 0 + height, w, h);

//                    imageView.backgroundColor = UIColor redColor;

                }else{

//                NSLog(@"图片宽比较大...");

//                    imageView.backgroundColor = UIColor yellowColor;

                    imageView.frame = CGRectMake(0, 0 + height, WIDTH - 30 * IPHONE6_W_SCALE, h);

                }

                height = height + h + 8*IPHONE6_H_SCALE;

NSLog(@"height=====%f", height);

//                                SDWebImageOptions options = SDWebImageRetryFailed | SDWebImageLowPriority;

                [imageView sd_setImageWithURL:_picArri placeholderImage:UIImage imageNamed:@"placeholder" completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

                }];

            }

        }else

        {

// 隐藏

            imageView.hidden = YES;

        }

    }

NSLog(@"allH%f", allH);

}

简单说几句:之前的代码没有先请求头视图中所有图片的大小然后传递过去而是在设置装图片的视图的大小的时候进行了所有图片大小的网络请求,在设置每个具体图片的大小进行了网络的请求,而且之前的图片大小请求方法会造成线程的阻塞,从而让各个模块在图片较多的情况下进行网络请求的时间较长而且不能与用户进行交互,用户体验并不好。在使用改进之后的代码即以上的代码,页面在即使图片较多的情况下加载速度也很快,用户体验明显好了许多,连本人都满意了许多。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年02月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档