iosobjective-cuitableviewuiviewcontrollerimageview

Change Image Frame Width IF image Width = 200


I need to change cell.image.frame to CGRectMake(105, 110, 10, 180) if the image width I get back from the API is 200.

Currently any size image I get I have at CGRectMake(0, 140, 320, 180).

So I just need to change the cell.image.frame only in the cases where the image width I get back is exactly 200.

I need help figuring out where to put the if statement, and what exactly to put in the if statement.

I can't seem to get it to work, so any help would be appreciated, thanks!

Below is the code:

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
        }];

Edit: I've tried putting this inside of the //Code part:

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

But it didn't work and also gave me a warning about capturing the cell strongly could lead to retain cycle.


Solution

  • Your code to check the image width makes no sense. You want:

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

    Update: To deal with the problem with the retain cycle you want something like this:

    __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);
        }
    }];