本文介绍了ios - UIImageView 上的 SizeToFit 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIImageView,它从 ios 文件系统上的文档目录加载图像.

问题是当我打电话

[imageView sizeToFit];

它不起作用.我认为这是因为那时图像尚未完全加载,因此在具有图像宽度之前调用了 sizeToFit 部分.高度.

有人能指出我修复的方向吗.

这是我的代码:

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

谢谢,阿什利

解决方案

检查图片大小:

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

您可能希望根据图像大小手动设置 imageView 框架,例如,如果图像太大而无法容纳 imageView 的 superview.>

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

解决方案

Check the image size:

NSLog(@"%@", NSStringFromCGSize(imageView.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 the imageView's superview.

这篇关于ios - UIImageView 上的 SizeToFit 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:45