繁体   English   中英

如何将UIImageView大小定义为UIImage分辨率?

[英]How to Define UIImageView size as UIImage resolution?

我有场景,我使用Web服务获取图像,所有图像都有不同的分辨率。 现在我的要求是我想要分辨每个图像并使用我想要定义UIImageView的大小以便我可以防止我的图像变得模糊

例如,图像分辨率如果326像素/英寸,则图像视图应该与该图像的大小完全无代表地表示。

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显示因此 - 使UIImageView 宽度/高度减小两倍 (这样每个UIImageView像素将由四个较小的UIImage像素组成,用于视网膜显示)。

如何检查视网膜显示:

https://stackoverflow.com/a/7607087/894671

如何检查图像大小(实际上没有在内存中加载图像):

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%工作....

让UIImageView通过利用contentMode属性为您的图像调整大小来完成工作。

您可能希望使用静态大小(“frame”属性)显示UIImageView,该大小表示要显示的图像的最大大小,并允许图像相对于其自身的特定大小要求在该帧内调整大小(整体尺寸,纵横比等)。 您可以通过掌握contentMode属性让UIImageView为您处理不同大小的图像做繁重的工作。 它有许多不同的设置,其中一个是UIViewContentModeScaleAspectFit,它会根据需要缩小你的图像以适应UIImageView,如果图像较小,它将只显示居中。 您可以使用该设置来获得所需的结果。

请注意,使用此方法,您无需处理与Retina显示相关的缩放问题。

要解决此问题,我们需要注意设备的显示分辨率。

例如,你有一张326ppi 分辨率的图像,它与iPhone4,iPhone4S和iPod 4th Gen相同。所以你可以简单地使用@Nit和@Peko建议的解决方案。 但对于其他设备(或这些设备上具有不同分辨率的图像),您需要应用数学来计算尺寸以便更好地显示。

现在假设您有260ppi (尺寸为W x H)图像,并且您希望在iPhone4S上显示它,因此每英寸中包含的信息小于iPhone的显示分辨率,因此我们需要通过缩小图像大小来调整它的大小由326/260因素。 所以现在你将使用的imageView的大小

imageViewWidth = W*(260/326);

imageViewHeight = H*(260/326);

一般来说:

resizeFactor = imageResolution/deviceDisplayResolution;
imageViewWidth = W*resizeFactor;
imageViewHeight = H*resizeFactor;

在这里我正在考虑当我们在imageView中设置图像并调整其大小时,它不会从图像中删除或添加像素,

根据您在问题正文中所述的要求,我相信您无需更改UIImageView大小。

使用以下代码行,图像可以完全没有任何模糊:

imageView.contentMode = UIViewContentModeScaleAspectFit;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM