简体   繁体   English

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

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

I have scenario, in which I am getting images using Web Service and all images are in different resolution. 我有场景,我使用Web服务获取图像,所有图像都有不同的分辨率。 Now my requirement is that I want resolution of each Images and using that I want to define size of UIImageView so I can prevent my Images from getting blurred 现在我的要求是我想要分辨每个图像并使用我想要定义UIImageView的大小以便我可以防止我的图像变得模糊

For example image resolution if 326 pixel/inch the imageview should be as size of that image can represent fully without any blur. 例如,图像分辨率如果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];

Image size IS it's resolution. 图像大小是它的分辨率。

Your problem might be - retina display! 你的问题可能是 - 视网膜显示!

Check for Retina display and thus - make UIImageView width/height twice smaller (so that each UIImageView pixel would consist of four smaller UIImage pixels for retina display). 检查Retina显示因此 - 使UIImageView 宽度/高度减小两倍 (这样每个UIImageView像素将由四个较小的UIImage像素组成,用于视网膜显示)。

How to check for retina display: 如何检查视网膜显示:

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

How to check image size (without actually loading image in memory): 如何检查图像大小(实际上没有在内存中加载图像):

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);
}

So - for example: 所以 - 例如:

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

Hope that helps! 希望有所帮助!

You can get image size using following code. 您可以使用以下代码获取图像大小。 So, first calculate downloaded image size and than make image view according to that. 因此,首先计算下载的图像大小,然后根据它来制作图像视图。

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

Hope, this will help you.. 希望这个能对您有所帮助..

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

Let the UIImageView do the work by utilizing the contentMode property to do your image resizing for you. 让UIImageView通过利用contentMode属性为您的图像调整大小来完成工作。

You probably want to be displaying your UIImageView with a static sizing (the "frame" property) that represents the maximum size of the image you want to display, and allowing the images to resize within that frame relative to their own particular size requirements (overall size, aspect ratio, etc.). 您可能希望使用静态大小(“frame”属性)显示UIImageView,该大小表示要显示的图像的最大大小,并允许图像相对于其自身的特定大小要求在该帧内调整大小(整体尺寸,纵横比等)。 You can let the UIImageView do the heavy lifting for you of dealing with different sized images by mastering the contentMode property. 您可以通过掌握contentMode属性让UIImageView为您处理不同大小的图像做繁重的工作。 It has many different settings, one of which is UIViewContentModeScaleAspectFit, which will downsize your image as necessary to fit within the UIImageView, which if the image is smaller, it will simply display centered. 它有许多不同的设置,其中一个是UIViewContentModeScaleAspectFit,它会根据需要缩小你的图像以适应UIImageView,如果图像较小,它将只显示居中。 You can play with the setting to get the results you want. 您可以使用该设置来获得所需的结果。

Note that with this approach, there is nothing special you need to do to deal with scaling issues associated with a Retina display. 请注意,使用此方法,您无需处理与Retina显示相关的缩放问题。

To solve this problem, we need to take care of the device's display resolution. 要解决此问题,我们需要注意设备的显示分辨率。 .

For example you have an image of resolution 326ppi which is same as of iPhone4, iPhone4S and iPod 4th Gen. So you can simply use solutions suggested by @Nit and @Peko. 例如,你有一张326ppi 分辨率的图像,它与iPhone4,iPhone4S和iPod 4th Gen相同。所以你可以简单地使用@Nit和@Peko建议的解决方案。 But for other devices(or for image with different resolution on these devices) you will need to apply maths to calculate size for better display. 但对于其他设备(或这些设备上具有不同分辨率的图像),您需要应用数学来计算尺寸以便更好地显示。

Now suppose you have 260ppi (with dimensions W x H) image and you wish to display it on iPhone4S, so as the information contained in it per inches is less than the display resolution of iPhone so we will need to resize it by reducing image size by 326/260 factor. 现在假设您有260ppi (尺寸为W x H)图像,并且您希望在iPhone4S上显示它,因此每英寸中包含的信息小于iPhone的显示分辨率,因此我们需要通过缩小图像大小来调整它的大小由326/260因素。 so now the size for imageView that you will use is 所以现在你将使用的imageView的大小

imageViewWidth = W*(260/326);

imageViewHeight = H*(260/326);

In general: 一般来说:

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

Here I am considering when we set an image in imageView and resize it, it does not removes or adds pixels from image, 在这里我正在考虑当我们在imageView中设置图像并调整其大小时,它不会从图像中删除或添加像素,

As per the requirement you stated in the question body, I believe you need not change UIImageView size. 根据您在问题正文中所述的要求,我相信您无需更改UIImageView大小。

Image can represent fully without any blur using this line of code: 使用以下代码行,图像可以完全没有任何模糊:

imageView.contentMode = UIViewContentModeScaleAspectFit;

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

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