Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 438
iDhaval Avatar asked May 04 '12 11:05

iDhaval


People also ask

How do I change the size of the UIImage in Swift?

Image resize function in swift as below. func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage { let size = image. size let widthRatio = targetSize. width / size.

How do you declare UIImage in Objective C?

For example: UIImage *img = [[UIImage alloc] init]; [img setImage:[UIImage imageNamed:@"anyImageName"]]; My UIImage object is declared in . h file and allocated in init method, I need to set image in viewDidLoad method.

Does UIImageView have intrinsic content size?

A vanilla UIView instance, for example, does not have an intrinsic content size. This means that you need to add four explicit constraints to describe the size and position of a UIView instance in a user interface.


2 Answers

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];
like image 186
peko Avatar answered Oct 26 '22 23:10

peko


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!

like image 34
Guntis Treulands Avatar answered Oct 26 '22 23:10

Guntis Treulands