我有一个场景,需要使用Web服务获取图片,所有的图片都有不同的分辨率。现在我的需求是想要获取每个图片的分辨率,并且根据这些信息定义UIImageView的大小,以便可以防止图片模糊。
例如,如果图片的分辨率为326像素/英寸,那么UIImageView的大小应该与图片完全匹配,而不会出现任何模糊。
我有一个场景,需要使用Web服务获取图片,所有的图片都有不同的分辨率。现在我的需求是想要获取每个图片的分辨率,并且根据这些信息定义UIImageView的大小,以便可以防止图片模糊。
例如,如果图片的分辨率为326像素/英寸,那么UIImageView的大小应该与图片完全匹配,而不会出现任何模糊。
UIImage *img = [UIImage imageNamed:@"foo.png"];
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
UIImageView *imgView = [[UIImageView alloc] initWithFrame:rect];
[imgView setImage:img];
图像大小就是它的分辨率。
你可能遇到的问题是 - Retina 显示屏!
检查是否为 Retina 显示屏,因此使 UIImageView 的 宽度/高度缩小两倍(这样每个 UIImageView 像素将由四个更小的 UIImage 像素组成以适应 Retina 显示屏)。
如何检查 Retina 显示屏:
https://dev59.com/MnA75IYBdhLWcg3weZDu#7607087
如何检查图像大小(不实际加载图像到内存中):
NSString *mFullPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
stringByAppendingPathComponent:@"imageName.png"];
NSURL *imageFileURL = [NSURL fileURLWithPath:mFullPath];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
if (imageSource == NULL)
{
// Error loading image ...
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache, nil];
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)options);
NSNumber *mImgWidth;
NSNumber *mImgHeight;
if (imageProperties)
{
//loaded image width
mImgWidth = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
//loaded image height
mImgHeight = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
CFRelease(imageProperties);
}
if (imageSource != NULL)
{
CFRelease(imageSource);
}
UIImageView *mImgView = [[UIImageView alloc] init];
[mImgView setImage:[UIImage imageNamed:@"imageName.png"]];
[[self view] addSubview:mImgView];
if ([UIScreen instancesRespondToSelector:@selector(scale)])
{
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale > 1.0)
{
//iphone retina screen
[mImgView setFrame:CGRectMake(0,0,[mImgWidth intValue]/2,[mImgHeight intValue]/2)];
}
else
{
//iphone screen
[mImgView setFrame:CGRectMake(0,0,[mImgWidth intValue],[mImgHeight intValue])];
}
}
您可以使用以下代码获取图像大小。因此,首先计算下载的图像大小,然后根据该大小创建图像视图。
UIImage *Yourimage = [UIImage imageNamed:@"image.png"];
CGFloat width = Yourimage.size.width;
CGFloat height = Yourimage.size.height;
UIImage *oldimage = [UIImage imageWithContentsOfFile:imagePath]; // or you can set from url with NSURL
CGSize imgSize = [oldimage size];
imgview.frame = CGRectMake(10, 10, imgSize.width,imgSize.height);
[imgview setImage:oldimage];
100%的工作效果……
通过利用contentMode属性,让UIImageView为您完成图像调整工作。
您可能希望使用静态大小(“frame”属性)显示UIImageView,该大小表示您要显示的图像的最大尺寸,并允许图像根据其自身特定的大小要求(总体大小、长宽比等)在该框架内进行调整。您可以让UIImageView为您处理不同大小的图像,掌握contentMode属性。它有许多不同的设置,其中之一是UIViewContentModeScaleAspectFit,它将缩小您的图像以适应UIImageView,如果图像较小,则仅显示居中。您可以调整设置以获得所需的结果。
请注意,使用此方法时,您无需特别处理与Retina显示相关的缩放问题。
imageViewWidth = W*(260/326);
imageViewHeight = H*(260/326);
一般来说:
resizeFactor = imageResolution/deviceDisplayResolution;
imageViewWidth = W*resizeFactor;
imageViewHeight = H*resizeFactor;
在这里我考虑的是当我们在imageView中设置并调整图像大小时,它不会从图像中删除或添加像素。
imageView.contentMode = UIViewContentModeScaleAspectFit;