1

I'm trying to replace an Image from an URL if it exists. The problem seems to be that I can't set the size of my UIImageView.

My Code looks like this:

UIImageView *partnerIcon = (UIImageView*)[cell viewWithTag:0];
NSURL *imageUrl = [NSURL URLWithString:[[@"http://www.fitpas.ch/coreapp/resources/images/center/" stringByAppendingString:[[result objectAtIndex:indexPath.row] objectForKey:@"cid"]] stringByAppendingString:@".jpg"]];

UIImage* partnerImage = [UIImage imageWithData: [NSData dataWithContentsOfURL: imageUrl]];

if (partnerImage != nil) {
    dispatch_async(dispatch_get_main_queue(), ^{

        //change image
        partnerIcon.image = partnerImage;
        partnerIcon.contentMode = UIViewContentModeScaleToFill;
    });
}

This results in:

UIImageView set Frame

In the Image above the last row partnerImage is nil and that shows how it should be.

I tried to scale the UIImage with

partnerIcon.image = [UIImage imageWithCGImage:partnerImage.CGImage 
    scale:1/100 orientation:partnerImage.imageOrientation];

but this won't change anything.

I also tried to set to change the dimension of the UIImageView with:

partnerIcon.bounds = CGRectMake(0, 0, 50, 50);

and also

partnerIcon.frame = CGRectMake(0, 0, 50, 50);

but this isn't working either.

fsulser
  • 853
  • 2
  • 10
  • 34
  • Are you using autolayout? – luk2302 Jan 10 '16 at 12:44
  • yes I do, but I think I need to, because there are some more views in the cell – fsulser Jan 10 '16 at 12:45
  • Yes, using auto layout is absolutely correct way to go. But you cannot change the `frame` if you use it, you need to connect the layout constraints as outlets and change them instead. – luk2302 Jan 10 '16 at 12:46
  • I don't understand why the frame is going to be changed automatically, basically I don't want to change it if it would work. I would just like to replace the image in the imageview, which does not seem to work. – fsulser Jan 10 '16 at 12:49
  • 2
    `[cell viewWithTag:0]` Tag zero is reserved by system. You should not use it. It is just an advice, not the solution to your question. – John Wong Jan 10 '16 at 12:51
  • Thanks to John Wong! This solution already solved it. – fsulser Jan 11 '16 at 13:01

1 Answers1

0

Set an explicit width and height constraint on the image view. If you don't do this then the content hugging and compression values will be used across all of the views to decide how big each should be based on the content size.

Wain
  • 118,658
  • 15
  • 128
  • 151