1

i have image which height is 600 and width is also 600 now i want to display that image Programmatically in UIImageView with 100x100. i try but image is not like as original image i add screen shoot of image First is 100x100 and Second is 200x200. second is fine.But first one is not like as original image. any idea what i do Pleaseenter image description here

In.h

@interface ViewController : UIViewController
{
    IBOutlet UIImageView *imageView1;
    IBOutlet UIImageView *imageView2;
}
@end

In.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    imageView1.frame=CGRectMake(100, 50, 100, 100);
    imageView2.frame=CGRectMake(50, 200, 200, 200);
    imageView1.image = [UIImage imageNamed:@"Untitled-7.png"];
    imageView2.image = [UIImage imageNamed:@"Untitled-7.png"];
}

original Image which is 600x600

Chirag Dj
  • 630
  • 2
  • 9
  • 27

12 Answers12

18

Please Try This

UIImage *originalImage = [UIImage imageNamed:@"5.png"];
        CGSize destinationSize = CGSizeMake(100, 100);
        UIGraphicsBeginImageContext(destinationSize);
        [originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImageView *newImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
        newImageView.image = newImage;
        [self.view addSubview:newImageView];
Nico Nimz
  • 268
  • 2
  • 15
  • it is working for square image , but if one need to resize the image in width = 100 and height = 50 then it shrinking . any help regarding this ?? – Shobhakar Tiwari Mar 31 '15 at 08:33
5

Try imageView1.contentMode = UIViewContentModeScaleAspectFit;

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
5

Use this Method :

- (UIImage*)imageWithImage:(UIImage*)image
              scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

Call the Method :

CGSize newSize = CGSizeMake(100.0,100.0);
[self imageWithImage:image scaledToSize:newSize];
Bhavin
  • 27,155
  • 11
  • 55
  • 94
3

Set the content mode

imageView2.contentMode = UIViewContentModeScaleAspectFit;
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
3

In swift 3

func createImage(withImage image: UIImage, forSize size: CGSize) -> UIImage? {
    UIGraphicsBeginImageContext(size)
    image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
    if let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() {
        UIGraphicsEndImageContext()
        return newImage;
    }
    return nil
}
alecnash
  • 1,750
  • 1
  • 18
  • 42
2

For Swift 4.2 onwards,

Use this and you'll get non-blur resized image in correct aspect ratio -

class func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
    let size = image.size

    let widthRatio  = targetSize.width  / size.width
    let heightRatio = targetSize.height / size.height

    // Figure out what our orientation is, and use that to form the rectangle
    var newSize: CGSize
    if(widthRatio > heightRatio) {
        newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
    } else {
        newSize = CGSize(width: size.width * widthRatio,  height: size.height * widthRatio)
    }

    // This is the rect that we've calculated out and this is what is actually used below
    let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

    //resizing the rect using the ImageContext
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
    image.draw(in: rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage!
}

According to Developer documentation, below method creates a bitmap-based graphics context with the specified options-

func UIGraphicsBeginImageContextWithOptions(_ size: CGSize, _ opaque: Bool, _ scale: CGFloat)

Parameters-

size: The size (measured in points) of the new bitmap context. This represents the size of the image returned by the UIGraphicsGetImageFromCurrentImageContext() function.

opaque: A Boolean flag indicating whether the bitmap is opaque.

scale: The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.

Reference- https://developer.apple.com/documentation/uikit/1623912-uigraphicsbeginimagecontextwitho

  • This is the way. You *have* to do the math to get the aspect ratio consistently. Every other example shows hard-coded values with very simple aspects; this one covers the gamut by doing the math. – ChrisH Jun 29 '22 at 04:06
0

initialize your imageView with image and then resize it

imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Untitled-7.png"]];
imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Untitled-7.png"]];
[imageView1 setFrame:CGRectMake(100, 50, 100, 100)];
[imageView2 setFrame:CGRectMake(50, 200, 200, 200)];

Try it and let me know if it works!

Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51
0

Try out this:-

- (void)viewDidLoad
{
    [super viewDidLoad];
    imageView1.frame=CGRectMake(100, 50, 100, 100);
    imageView2.frame=CGRectMake(50, 200, 200, 200);
    imageView1.contentMode = UIViewContentModeScaleAspectFit;
    imageView2.contentMode = UIViewContentModeScaleAspectFit;
    imageView1.image = [UIImage imageNamed:@"Untitled-7.png"];
    imageView2.image = [UIImage imageNamed:@"Untitled-7.png"];
}

enjoy.

Kundan
  • 3,084
  • 2
  • 28
  • 65
0

Try changing this:

imageView1.image = [UIImage imageNamed:@"Untitled-7.png"];

To this:

imageView1.image = [[UIImage imageNamed:@"Untitled-7.png"] resizableImageWithCapInsets:UIEdgeInsetsZero
                                                                          resizingMode:UIImageResizingModeStretch];
drewmm
  • 737
  • 6
  • 17
0

I like using a category on UIImage, with a method like this:

// Returns a rescaled copy of the image, taking into account its orientation
// The image will be scaled disproportionately if necessary to fit the bounds specified by the parameter
- (UIImage *)resizedImage:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width * self.scale, newSize.height * self.scale));

    UIImage *newImage;
    UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
    newImage = [UIImage imageWithCGImage:[self CGImage] scale:0.0 orientation:self.imageOrientation];
    [newImage drawInRect:newRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

See this updated repo and this blog post.

This works well for resizing PDF images, especially if you need to stick one in a UIBarButtonItem.

Andrew Johnson
  • 13,108
  • 13
  • 75
  • 116
0

Hey if you are already using Alamofire Image Library then you can use following code:

UIImage(named:"image").af_imageScaled(to: CGSize(width: 100, height: 100))
kbokdia
  • 439
  • 5
  • 11