MAK113
MAK113

Reputation: 1452

Resize uiimage not give me exact new size after resize it

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

Upvotes: 1

Views: 170

Answers (2)

user4261201
user4261201

Reputation: 2342

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;

  }

Upvotes: 0

las
las

Reputation: 288

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.

Upvotes: 1

Related Questions