更改图像帧宽度 IF 图像宽度 = 200


如果我

从 API 返回的图像宽度为 200,我需要将cell.image.frame更改为 CGRectMake(105, 110, 10, 180)

目前我得到的任何尺寸的图像我都有CGRectMake(0, 140, 320, 180).

所以我只需要在我返回的图像宽度正好是 200 的情况下更改cell.image.frame

我需要帮助弄清楚将if语句放在哪里,以及究竟要放入if语句中的内容

我似乎无法让它工作,所以任何帮助将不胜感激,谢谢!

下面是代码:

WebListCell.m

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(0, 140, 320, 180);   
}

WebListViewController.m

Images *imageLocal = [feedLocal.images objectAtIndex:0];
NSString *imageURL = [NSString stringWithFormat:@"%@", imageLocal.url];
[cell.imageView setImageWithURL:[NSURL URLWithString:imageURL]
                   placeholderImage:[UIImage imageNamed:@"img.gif"]
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
        {
        // Code
        }];

编辑:我试过把它放在//Code部分:

if(imageWith == [NSString stringWithFormat:@"200"])
         {
           cell.imageView.frame = CGRectMake(105, 110, 10, 180);
         }

但它没有奏效,也给了我一个警告,强烈捕获细胞可能会导致保留周期。

检查图像宽度的代码毫无意义。你想要:

if(image.size.width == 200) {
    cell.imageView.frame = CGRectMake(105, 110, 10, 180);
}

更新:要处理保留周期的问题,您需要这样的东西:

__weak UITableViewCell *weakcell = cell;
[cell.imageView setImageWithURL:[NSURL URLWithString:imageURL]
                      placeholderImage:[UIImage imageNamed:@"img.gif"]
                      completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
    if(image.size.width == 200) {
        weakcell.imageView.frame = CGRectMake(105, 110, 10, 180);
    }
}];

你应该把 if 语句放在完成块中,因为只有在那里你才有图像分辨率。

最新更新