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

时间:2012-05-04 11:21:23

标签: iphone ipad uiimageview uiimage

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

例如图像分辨率,如果326像素/英寸,则图像视图应该与该图像的大小完全没有任何模糊一样。

7 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

图像大小是它的分辨率。

您的问题可能是 - 视网膜显示!

检查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])];
    }
}

希望有所帮助!

答案 2 :(得分:2)

您可以使用以下代码获取图像大小。因此,首先计算下载的图像尺寸,然后根据该图像进行图像查看。

UIImage *Yourimage = [UIImage imageNamed:@"image.png"];
CGFloat width = Yourimage.size.width;
CGFloat height = Yourimage.size.height;

希望,这会对你有帮助..

答案 3 :(得分:2)

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

答案 4 :(得分:1)

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

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

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

答案 5 :(得分:1)

要解决此问题,我们需要处理device's display resolution.

例如,您的图像为resolution 326ppi ,与iPhone4,iPhone4S和iPod 4th Gen相同。因此您只需使用建议的解决方案通过@Nit和@Peko。但对于其他设备(或这些设备上具有不同分辨率的图像),您需要将数学应用于calculate size以便更好地显示。

现在假设您有 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中设置图像并调整其大小时,它不会从图像中删除或添加像素,

答案 6 :(得分:0)

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

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

imageView.contentMode = UIViewContentModeScaleAspectFit;
相关问题