计算图片的宽和高 动态设置图片的位置

  切记 : 计算图片的高度和宽度应该在请求数据的时候获取.然后保存起来 否则的话会造成卡顿现象     
 
方法优化:
    WidthAndHeight *wAndH = _widthAndHeightDataArray[indexPath.row];
   
    CGFloat width = wAndH.widthPic;
   
    CGFloat height = wAndH.heightPic;
   
    CGRect picBackViewFrame = cell.picBackView.frame;
    cell.picBackView.frame = CGRectMake(0, picBackViewFrame.origin.y, SCREEN_WIDTH, SCREEN_WIDTH);
    cell.postPhoto.frame = CGRectMake(width / 2 > SCREEN_WIDTH ? 0 : ((SCREEN_WIDTH - width / 2) / 2), height / 2 > SCREEN_WIDTH ? 0 : ((SCREEN_WIDTH - height / 2) / 2), width / 2 > SCREEN_WIDTH ? SCREEN_WIDTH : (width / 2), height / 2 > SCREEN_WIDTH ? SCREEN_WIDTH : (height / 2));
 
以下舍弃
#pragma  mark - requestMore
- (void)requestMore {
    [API selectNoteContentFrom:from to:to completion:^(NSError *error, id responseData) {
        if (!error) {
           
            for (NSDictionary *dic in responseData[@"result"]) {
                FindContent *find = [[FindContent alloc] init];
                [find setValuesForKeysWithDictionary:dic];
               
                UIImage *image = [[UIImage alloc] initWithData:[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:find.imgList[0]]]];
                WidthAndHeight *wAndH = [[WidthAndHeight alloc] init];
                CGFloat width  = image.size.width;
                CGFloat height = image.size.height;
                wAndH.widthPic = width;
                wAndH.heightPic = height;
                [_widthAndHeightDataArray addObject:wAndH];
                NSLog(@"**************************************%@", wAndH);
                [_noteArray addObject:find];
            }
            [self.tableView reloadData];
        }
    }];
}
UIView*picBacgroundView;
        UIImageView *contentPic;
        for (int i = 0; i < imgArray.count; i++) {
       
    WidthAndHeight *wAndH = _widthAndHeightDataArray[indexPath.row];
   
    CGFloat width = wAndH.widthPic;
   
    CGFloat height = wAndH.heightPic;
            if (iPhone4) {
                if (width / 2 > SCREEN_WIDTH) {
                    picBacgroundView.frame = CGRectMake(0, photoImage.frame.origin.y + photoImage.frame.size.height + 10 + SCREEN_WIDTH * i + 10 * i, SCREEN_WIDTH, SCREEN_WIDTH);
                    contentPic.frame = CGRectMake(0, 0 , SCREEN_WIDTH, SCREEN_WIDTH);
                } else {
                    picBacgroundView.frame = CGRectMake(0, photoImage.frame.origin.y + photoImage.frame.size.height + 10 + SCREEN_WIDTH * i + 10 * i, SCREEN_WIDTH, SCREEN_WIDTH);
                    contentPic.frame = CGRectMake((SCREEN_WIDTH - width / 2) / 2, (SCREEN_WIDTH - height / 2) / 2 , width / 2, height / 2);
                }
            } else if (iPhone5) {
                if (width / 2 > SCREEN_WIDTH) {
                    picBacgroundView.frame = CGRectMake(0, photoImage.frame.origin.y + photoImage.frame.size.height + 10 + SCREEN_WIDTH * i + 10 * i, SCREEN_WIDTH, SCREEN_WIDTH);
                    contentPic.frame = CGRectMake(0, 0 , SCREEN_WIDTH, SCREEN_WIDTH);
                } else {
                    picBacgroundView.frame = CGRectMake(0, photoImage.frame.origin.y + photoImage.frame.size.height + 10 + SCREEN_WIDTH * i + 10 * i, SCREEN_WIDTH, SCREEN_WIDTH);
                    contentPic.frame = CGRectMake((SCREEN_WIDTH - width / 2) / 2, (SCREEN_WIDTH - height / 2) / 2 , width / 2, height / 2);
                }
            } else if (iPhone6) {
                if (width / 2 > SCREEN_WIDTH) {
                    picBacgroundView.frame = CGRectMake(0, photoImage.frame.origin.y + photoImage.frame.size.height + 10 + SCREEN_WIDTH * i + 10 * i, SCREEN_WIDTH, SCREEN_WIDTH);
                    contentPic.frame = CGRectMake(0, 0 , SCREEN_WIDTH, SCREEN_WIDTH);
                } else {
                    picBacgroundView.frame = CGRectMake(0, photoImage.frame.origin.y + photoImage.frame.size.height + 10 + SCREEN_WIDTH * i + 10 * i, SCREEN_WIDTH, SCREEN_WIDTH);
                    contentPic.frame = CGRectMake((SCREEN_WIDTH - width / 2) / 2, (SCREEN_WIDTH - height / 2) / 2 , width / 2, height / 2);
                }
            } else {
                picBacgroundView.frame = CGRectMake(0, photoImage.frame.origin.y + photoImage.frame.size.height + 10 + SCREEN_WIDTH * i + 10 * i, SCREEN_WIDTH, SCREEN_WIDTH);
                contentPic.frame = CGRectMake((SCREEN_WIDTH - width / 2) / 2, (SCREEN_WIDTH - height / 2) / 2 , width / 2, height / 2);
            }
            [contentPic sd_setImageWithURL:[NSURL URLWithString:self.findContent.imgList[i]]];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pressPic:)];
            [contentPic addGestureRecognizer:tap];
            contentPic.userInteractionEnabled = YES;
            [picBacgroundView addSubview:contentPic];
        }
原文地址:https://www.cnblogs.com/tian-sun/p/5019944.html