gsdios / SDAutoLayout

One line of code to implement automatic layout. 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。The most easy way for autoLayout. Based on runtime.
MIT License
5.9k stars 1.28k forks source link

9宫格图片在加载一张网络图片时 无法显示 代码如下 100的地方请自行根据图片设置 #23

Open shuaiyinoo opened 8 years ago

shuaiyinoo commented 8 years ago
AlianOL commented 4 years ago
  • (void)setPicPathStringsArray:(NSArray )picPathStringsArray { _picPathStringsArray = picPathStringsArray; [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; if (_picPathStringsArray.count == 0) { self.height = 0; self.fixedHeight = @(0); return; } CGFloat itemW = [self itemWidthForPicPathArray:_picPathStringsArray]; CGFloat itemH = 0; if (_picPathStringsArray.count == 1) { UIImage image = [UIImage imageNamed:_picPathStringsArray.firstObject]; if (image.size.width) { itemH = image.size.height / image.size.width itemW; } } else { itemH = itemW; } long perRowItemCount = [self perRowItemCountForPicPathArray:_picPathStringsArray]; CGFloat margin = 10; [_picPathStringsArray enumerateObjectsUsingBlock:^(NSString _Nonnull obj, NSUInteger idx, BOOL _Nonnull stop) { long columnIndex = idx % perRowItemCount; long rowIndex = idx / perRowItemCount; UIImageView imageView = [UIImageView new];

    //加载网络图片
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:obj] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
    //判断是多张图片还是一张图片
    if (_picPathStringsArray.count > 1) {
      imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
    }else{
      imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), 100, 100);
    }
    
    [self addSubview:imageView];
    
    imageView.userInteractionEnabled = YES;
    imageView.tag = idx;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
    [imageView addGestureRecognizer:tap];

    }]; CGFloat w = perRowItemCount itemW + (perRowItemCount - 1) margin; int columnCount = ceilf(_picPathStringsArray.count 1.0 / perRowItemCount); CGFloat h = columnCount itemH + (columnCount - 1) * margin; //NSLog(@"高度为%f 宽度为%f",h,w); if (_picPathStringsArray.count == 1) { h = 100; } self.width = w; self.height = h; self.fixedHeight = @(h); self.fixedWith = @(w); }

老哥,,你加载网络图片整个页面滑动卡顿么?