iphone - Resize UIImage without change aspect ratio | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

iphone - Resize UIImage without change aspect ratio

Monday

iPhone Resizing a UIImage:
  This is the sample code for resize an image without change it's original aspect ratio.

Copy and  paste the following code into your .h file

// Change image size without change image ratio
-(UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize;



This is the method for resize the image, copy and  paste the following code into your .m file
-(UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize { 
   
    float width = newSize.width;
    float height = newSize.height;
   
    UIGraphicsBeginImageContext(newSize);
    CGRect rect = CGRectMake(0, 0, width, height);
   
    float widthRatio = image.size.width / width;
    float heightRatio = image.size.height / height;
    float divisor = widthRatio > heightRatio ? widthRatio : heightRatio;
   
    width = image.size.width / divisor;
    height = image.size.height / divisor;
   
    rect.size.width  = width;
    rect.size.height = height;
   
    if(height < width)
        rect.origin.y = height / 3;
    [image drawInRect: rect];
   
    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();  
   
    return smallImage;
   
}

And finally this is the method to call image resize function.

  UIImage *imageCC= [UIImage imageNamed:@"codingcluster.png"];      
  // Pass your image size as paramater
   UIImage *buttonBk = [self scaleImage:imageCC toSize:CGSizeMake(40.0,40.0)];

2 comments:

Anonymous said...

Goοԁ аnswer bаck in гeturn
of thіs mаtter wіth genuine aгguments anԁ describing all about that.
Here is my blog : buy facebook fans cheap

Unknown said...

Hi. Thanks for official information. I am real and continue user of Facebook. It’s a biggest social site and more powerful for business. If we want more traffic then buy Facebook fans and increase your business.

Post a Comment

Share your thoughts here...

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets