0
votes

I'm loading an UIImage into a UIImageView and setting the dimensions of the image to be the same as of the UIImageView which was created in storyboard and is 60x60 by default. Code-wisely, everything works, the debug says that the dimensions of image and imageView are both 60px, however looking at the simulator, you can obviously see a rectangle rather than a box. Where is the problem?

Image loading code:

- (void)viewWillAppear:(BOOL)animated
{   
    NSString *userImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/TheOfficialKanyeWest/picture?"];
    NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:userImageURL]];
    UIImage* profilePic = [UIImage imageWithData:data];
    profilePic = [self imageWithImage:profilePic scaledToSize:profilePictureImageView.frame.size];
    [profilePictureImageView setImage:profilePic];
    //[profilePictureImageView sizeToFit];

    NSLog(@"\n\nView:\nWidth: %f\nHeight: %f\n\nImage:\nWidth: %f\nHeight: %f\n", profilePictureImageView.frame.size.width, profilePictureImageView.frame.size.height, profilePic.size.width, profilePic.size.height);
}

Custom function for resizing image:

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

Also, the debug I put in the ViewDidAppear gives me:

View:
Width: 60.000000
Height: 60.000000

Image:
Width: 60.000000
Height: 60.000000

Edit: Forgot to add screenshot

screenshot

Solution

The code was absolutely fine. I failed to notice that my automatic constraints were causing the imageView to resize to the odd rectangular shape. I added my constraints manually and now it works well.

4
add screenshot of simulator.nikhil84
do you have any constraints applied to imageView ?harsha yarabarla
nikhil84 - thanks for the comment, I forgot to show you what I was seeing harsha - Yeah, the imageView was created in storyboard so I applied the automaticconstraints to keep it in this position irregardless of the resolutionSam
then did you pin width and height of the imageView using constraints to 60 X 60 ?.harsha yarabarla
set 60x60 constraints instead of automatically setaaisataev

4 Answers

1
votes

Pin width and height constraints of the imageView to 60 and 60 in story board don't use automatic constraints.

1
votes

I have tried your code, in coding its absolutely right, but in storyboard there is any mistakes in autolayouts..enter image description here

0
votes

Move the code from viewWillAppear to viewDidAppear. In viewWillAppear your UIImageView is not set to its final frame.

0
votes

Try your code in

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
// put your code to here to set frame and size of imageview.
}