// 获取所有图片的宽和高
for (int i = 0; i < imageArr.count; i ++) {
CGSize size = [UIImageView downloadImageSizeWithURL:[NSURL URLWithString:imageArr[i]]];
if (size.width == 0.0 || size.height == 0.0) {
NSURL *url = [NSURL URLWithString:imageArr[i]];
//因为这个方法在子线程(全局队列)中执行,所以不需要考虑死线程的问题
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];
nameDic[NSFontAttributeName] = 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];
timeDic[NSFontAttributeName] = 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];
titleDic[NSFontAttributeName] = 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(@"_heightArr[i]===%@", _heightArr[i]);
w = [_widthArr[i] floatValue] * IPHONE6_W_SCALE;
h = [_heightArr[i] 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 = [_heightArr[i] floatValue] * IPHONE6_W_SCALE;
w = [_widthArr[i] 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:picArr[i]];
}
_picArr = (NSArray *)arr;
}
NSUInteger counts = self.subviews.count;
CGFloat height = 0;
CGFloat allH = 0;
for (int i = 0; i < counts; i ++) {
UIImageView * imageView = self.subviews[i];
// 9张图片以内显示上传的所有图片
if (i < _picArr.count) {
imageView.hidden = NO;
if (picArr.count > 0) {
CGFloat h; // 图片的高度
CGFloat w; // 图片的宽度
// CGSize size = [UIImageView downloadImageSizeWithURL:[NSURL URLWithString:_picArr[i]]];
NSLog(@"_heightArr[i]===%@", _heightArr[i]);
h = [_heightArr[i] floatValue] * IPHONE6_W_SCALE;
w = [_widthArr[i] 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 = [_heightArr[i] floatValue] * IPHONE6_W_SCALE;
w = [_widthArr[i] 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:_picArr[i] placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}];
}
}else
{
// 隐藏
imageView.hidden = YES;
}
}
NSLog(@"allH%f", allH);
}
简单说几句:之前的代码没有先请求头视图中所有图片的大小然后传递过去而是在设置装图片的视图的大小的时候进行了所有图片大小的网络请求,在设置每个具体图片的大小进行了网络的请求,而且之前的图片大小请求方法会造成线程的阻塞,从而让各个模块在图片较多的情况下进行网络请求的时间较长而且不能与用户进行交互,用户体验并不好。在使用改进之后的代码即以上的代码,页面在即使图片较多的情况下加载速度也很快,用户体验明显好了许多,连本人都满意了许多。