Ios - SizeToFit for UIImageView not working

I have a UIImageView that is loaded with images from a document directory in the ios file system.

The problem is that when I call

[imageView sizeToFit];

This does not work. I think this is due to the fact that the image did not load completely to this point, and therefore part of sizeToFit is called before it has the width and height of the image.

Can someone point me towards correction, please.

Here is my code:

imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageWithContentsOfFile:[FileOperations getLibraryPath:[NSString stringWithFormat:@"%d.png", listData.imageId]]];
[imageView sizeToFit];
[imageScroll setContentOffset:CGPointMake(0, 0)];
imageScroll.contentSize = imageView.frame.size;
imageScroll.decelerationRate = UIScrollViewDecelerationRateFast;
imageScroll.minimumZoomScale = 1;
imageScroll.maximumZoomScale = 2;
[imageScroll setZoomScale:imageScroll.minimumZoomScale];
[imageScroll addSubview:imageView];

Thanks Ashley

+3
source share
3 answers

Check image size:

NSLog(@"%@", NSStringFromCGSize(imageView.image.size));

You might want to set the frame imageViewbased on the size of the image manually, for example, if the image is too large to fit the label imageView.

+3
UIImageView *myImage = [[UIImageView alloc]init];
myImage.contentMode = UIViewContentModeScaleAspectFit;
myImage.image = [UIImage imageName:@"image.png"];
[self.view addSubview:myImage];
+2

sizeToFit , , UIImage .

UIImage imageWithContentsOfFile:returns a (fully loaded) link to UIImageor nil. Are you sure you are going the right way?

0
source

All Articles