1
- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));    //CGSize is 51*51
CGImageRef imageRef = image.CGImage;

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

CGContextConcatCTM(context, flipVertical);
// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef);

// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

CGImageRelease(newImageRef);
UIGraphicsEndImageContext();
 NSLog(@"size of resizeimage is %@",NSStringFromCGSize(newImage.size));    // here i get 102*102
return newImage;
}

I enter cgsize that is 51*51,

and after resize when i check the size its give me 102*102.

why?Please solve my problem.

MAC113
  • 1,444
  • 12
  • 19
  • U try to search answer on ur question? http://stackoverflow.com/a/2658801/887325 – Bimawa Mar 02 '15 at 12:09
  • size 51*51 refers to image for a non retina version. You should be using retina display, therefore, you will get the value as 2x. which is technically correct. – Jasmeet Singh Mar 02 '15 at 12:24
  • Thank you so much Jasmeet Singh . You save my time . Thanks a lot!!! – MAC113 Mar 03 '15 at 03:52

2 Answers2

1

Look at your UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);, from Apple documentation the scale parameter means :

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.

So if it's a retina display, it's normal.

las
  • 288
  • 2
  • 8
0

I have followed this method to resize image

-(UIImage *)resizeImage:(UIImage *)image newSize:(CGSize)newSize
{
    float actualHeight = image.size.height;
    float actualWidth = image.size.width;
    float maxHeight = newSize.height;  
    float maxWidth = newSize.width;  
    float imgRatio = actualWidth/actualHeight;
    float maxRatio = maxWidth/maxHeight;

    if (actualHeight > maxHeight || actualWidth > maxWidth)
    {
       if(imgRatio < maxRatio)
       {
        //adjust width according to maxHeight
        imgRatio = maxHeight / actualHeight;
        actualWidth = imgRatio * actualWidth;
        actualHeight = maxHeight;
       }
       else if(imgRatio > maxRatio)
       {
        //adjust height according to maxWidth
        imgRatio = maxWidth / actualWidth;
        actualHeight = imgRatio * actualHeight;
        actualWidth = maxWidth;
       }
       else
       {
        actualHeight = maxHeight;
        actualWidth = maxWidth;
       }
    }

   CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
   UIGraphicsBeginImageContext(rect.size);
   [image drawInRect:rect];
   UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();


   NSLog(@"size of resizeimage is %@",NSStringFromCGSize(img.size));
   NSLog(@"size of original is %@",NSStringFromCGSize(image.size));


   return img;

  }
user4261201
  • 2,324
  • 19
  • 26