简体   繁体   中英

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. 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

For example image resolution if 326 pixel/inch the imageview should be as size of that image can represent fully without any blur.

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).

How to check for retina display:

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 ....

Let the UIImageView do the work by utilizing the contentMode property to do your image resizing for you.

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.). You can let the UIImageView do the heavy lifting for you of dealing with different sized images by mastering the contentMode property. 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. 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.

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. 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. so now the size for imageView that you will use is

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,

As per the requirement you stated in the question body, I believe you need not change UIImageView size.

Image can represent fully without any blur using this line of code:

imageView.contentMode = UIViewContentModeScaleAspectFit;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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