I have a UIImageView which loads in images from the documents directory on the ios file system .
The problem is that when I call
[imageView sizeToFit];
It does not work. I think it is because the image hasn't loaded fully at that point and so the sizeToFit part is called before it has the image width & height.
Could someone point me in the direction of a fix 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
sizeToFit
should work in your situation, but fails if the UIImage is nil.UIImage's
imageWithContentsOfFile:
returns a (completely loaded) reference to aUIImage
ornil
. Are you sure that you're passing the right path?Check the image size:
You may want to set
imageView
frame based on the image size manually, for example, if the image is too large to fit theimageView
's superview.