How to define UIImageView size as UIImage resolution?

I have a scenario where I get images using a web service, and all images have different resolutions. Now my requirement is that I want the resolution of each image and use that I want to determine the size of the UIImageView so that I can prevent my images from getting blurry.

For example, the resolution of the image, if the image size of 326 pixels / inch should be the same as the size of this image, can be fully displayed without blurring.

+7
source share
7 answers

To solve this problem, we need to take care of the screen resolution of the device. .

For example, you have an image resolution of 326ppi , which is the same as the iPhone4, iPhone4S and iPod 4th generation. Therefore, you can simply use the solutions offered by @Nit and @Peko. But for other devices (or for images with different resolutions on these devices) you need to apply math to calculate the size for better display.

Now suppose you have 260ppi (with dimensions W x H) and you want to display it on iPhone4S, since the information contained in it is less than the iPhone screen resolution, so we will need to resize it by reducing the image size at 326/260. so now the size for the imageView you will be using is

imageViewWidth = W*(260/326); imageViewHeight = H*(260/326); 

Generally:

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

Here I look at when we set the image to imageView and resize it, it does not remove or add pixels from the image,

+1
source
 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]; 
+5
source

Image Size This is the resolution.

Your problem may be retinal imaging!

Check the Retina display and therefore make the UIImageView half the width / height (so that each UIImageView pixel will consist of four smaller UIImage pixels to display the retina).

How to check retinal display:

stack overflow

How to check image size (without actually loading the image into 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 this helps!

+4
source

You can get the image size using the following code. So, first calculate the loaded image size, and then make the image accordingly.

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

Hope this helps you.

+3
source
  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% works ....

+3
source

Let UIImageView do the work using the contentMode property to resize the image for you.

You probably want to display your UIImageView with a static size (the "frame" property), which represents the maximum size of the image you want to display and allows you to resize images within this frame relative to their own size requirements (total size, aspect ratio, etc.) .d.). You can let UIImageView do the hard work of working with images of different sizes by mastering the contentMode property. It has many different settings, one of which is UIViewContentModeScaleAspectFit, which will reduce the size of your image as necessary so that it matches the UIImageView, which, if the image is smaller, will simply be displayed in the center. You can play with the setting to get the desired results.

Please note that with this approach, there is nothing special to deal with the scaling problems associated with the Retina display.

+1
source

According to the requirement specified in the question body, I believe that you do not need to resize the UIImageView.

The image can be fully displayed without blurring using this line of code:

 imageView.contentMode = UIViewContentModeScaleAspectFit; 
0
source

All Articles