Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, April 30, 2016

Resize UIImage by keeping Aspect ratio and width

Resize UIImage by keeping Aspect ratio and width


I seen in many posts for resizing the image by keeping aspect ratio. These functions uses the fixed points(Width and Height) for RECT while resizing. But in my project, I need to resize the view based on the Width alone, Height should be taken automatically based on the aspect ratio. anyone help me to achieve this.

Answer by Srikar Appal for Resize UIImage by keeping Aspect ratio and width


The simplest way is to set the frame of your UIImageView and set the contentMode to one of the resizing options.

The code goes like this - This can be used as an utility 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;  }  

Answer by Maverick1st for Resize UIImage by keeping Aspect ratio and width


The method of Srikar works very well, if you know both height and width of your new Size. If you for example know only the width you want to scale to and don't care about the height you first have to calculate the scale factor of the height.

+(UIImage*)imageWithImage: (UIImage*) sourceImage scaledToWidth: (float) i_width  {      float oldWidth = sourceImage.size.width;      float scaleFactor = i_width / oldWidth;        float newHeight = sourceImage.size.height * scaleFactor;      float newWidth = oldWidth * scaleFactor;        UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight));      [sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];      UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();          UIGraphicsEndImageContext();      return newImage;  }  

Answer by Ryan for Resize UIImage by keeping Aspect ratio and width


If you don't happen to know if the image will be portrait or landscape (e.g user takes pic with camera), I created another method that takes max width and height parameters.

Lets say you have a UIImage *myLargeImage which is a 4:3 ratio.

UIImage *myResizedImage = [ImageUtilities imageWithImage:myLargeImage                                           scaledToMaxWidth:1024                                                  maxHeight:1024];  

The resized UIImage will be 1024x768 if landscape; 768x1024 if portrait. This method will also generate higher res images for retina display.

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)size {      if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {          UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);      } else {          UIGraphicsBeginImageContext(size);      }      [image drawInRect:CGRectMake(0, 0, size.width, size.height)];      UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();          UIGraphicsEndImageContext();        return newImage;  }    + (UIImage *)imageWithImage:(UIImage *)image scaledToMaxWidth:(CGFloat)width maxHeight:(CGFloat)height {      CGFloat oldWidth = image.size.width;      CGFloat oldHeight = image.size.height;        CGFloat scaleFactor = (oldWidth > oldHeight) ? width / oldWidth : height / oldHeight;        CGFloat newHeight = oldHeight * scaleFactor;      CGFloat newWidth = oldWidth * scaleFactor;      CGSize newSize = CGSizeMake(newWidth, newHeight);        return [self imageWithImage:image scaledToSize:newSize];  }  

Answer by mariusLAN for Resize UIImage by keeping Aspect ratio and width


Just import AVFoundation and use AVMakeRectWithAspectRatioInsideRect(CGRectCurrentSize, CGRectMake(0, 0, YOUR_WIDTH, CGFLOAT_MAX)

Answer by TheCodingArt for Resize UIImage by keeping Aspect ratio and width


To improve on Ryan's answer:

   + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)size {  CGFloat oldWidth = image.size.width;          CGFloat oldHeight = image.size.height;    //You may need to take some retina adjustments into consideration here          CGFloat scaleFactor = (oldWidth > oldHeight) ? width / oldWidth : height / oldHeight;    return [UIImage imageWithCGImage:image.CGImage scale:scaleFactor orientation:UIImageOrientationUp];      }  

Answer by Jnos for Resize UIImage by keeping Aspect ratio and width


thanks @Maverick1st the algorithm, I implemented it to Swift, in my case height is the input parameter

class func resizeImage(image: UIImage, newHeight: CGFloat) -> UIImage {        let scale = newHeight / image.size.height      let newWidth = image.size.width * scale      UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))      image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))      let newImage = UIGraphicsGetImageFromCurrentImageContext()      UIGraphicsEndImageContext()        return
newImage }

Answer by mohamede1945 for Resize UIImage by keeping Aspect ratio and width


This method is a category on UIImage. Does scale to fit in few lines of code using AVFoundation. Don't forget to import #import .

@implementation UIImage (Helper)    - (UIImage *)imageScaledToFitToSize:(CGSize)size  {      CGRect scaledRect = AVMakeRectWithAspectRatioInsideRect(self.size, CGRectMake(0, 0, size.width, size.height));      UIGraphicsBeginImageContextWithOptions(size, NO, 0);      [self drawInRect:scaledRect];      UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();      UIGraphicsEndImageContext();      return scaledImage;  }    @end  

Answer by Daniel Krom for Resize UIImage by keeping Aspect ratio and width


Ryan's Solution @Ryan in swift code

use:

 func imageWithSize(image: UIImage,size: CGSize)->UIImage{      if UIScreen.mainScreen().respondsToSelector("scale"){          UIGraphicsBeginImageContextWithOptions(size,false,UIScreen.mainScreen().scale);      }      else      {           UIGraphicsBeginImageContext(size);      }        image.drawInRect(CGRectMake(0, 0, size.width, size.height));      var newImage = UIGraphicsGetImageFromCurrentImageContext();      UIGraphicsEndImageContext();        return newImage;  }    //Summon this function VVV  func resizeImageWithAspect(image: UIImage,scaledToMaxWidth width:CGFloat,maxHeight height :CGFloat)->UIImage  {      let oldWidth = image.size.width;      let oldHeight = image.size.height;        let scaleFactor = (oldWidth > oldHeight) ? width / oldWidth : height / oldHeight;        let newHeight = oldHeight * scaleFactor;      let newWidth = oldWidth * scaleFactor;      let newSize = CGSizeMake(newWidth, newHeight);        return imageWithSize(image, size: newSize);  }  

Answer by RDC for Resize UIImage by keeping Aspect ratio and width


Well, we have few good answers here for resizing image, but I just modify as per my need. hope this help someone like me.

My Requirement was

  1. If image width is more than 1920, resize it with 1920 width and maintaining height with original aspect ratio.
  2. If image height is more than 1080, resize it with 1080 height and maintaining width with original aspect ratio.

 if (originalImage.size.width > 1920)   {          CGSize newSize;          newSize.width = 1920;          newSize.height = (1920 * originalImage.size.height) / originalImage.size.width;          originalImage = [ProfileEditExperienceViewController imageWithImage:originalImage scaledToSize:newSize];           }     if (originalImage.size.height > 1080)   {              CGSize newSize;              newSize.width = (1080 * originalImage.size.width) / originalImage.size.height;              newSize.height = 1080;              originalImage = [ProfileEditExperienceViewController imageWithImage:originalImage scaledToSize:newSize];     }    + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize  {          UIGraphicsBeginImageContext(newSize);          [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];          UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();          UIGraphicsEndImageContext();          return newImage;  }  

Thanks to @Srikar Appal , for resizing I have used his method.

You may like to check this as well for resize calculation.

Answer by Siddhant Saxena for Resize UIImage by keeping Aspect ratio and width


I have solved it in a much simpler way

UIImage *placeholder = [UIImage imageNamed:@"OriginalImage.png"];  self.yourImageview.image = [UIImage imageWithCGImage:[placeholder CGImage] scale:(placeholder.scale * 1.5)                                    orientation:(placeholder.imageOrientation)];  

multiplier of the scale will define the scalling of the image,more the multiplier smaller the image. so You can check what fits your screen.

Or you can also get the multiplire by dividing imagewidth/screenwidth.

Answer by Alessandro Ornano for Resize UIImage by keeping Aspect ratio and width


Best answer Maverick 1st's translated to swift (working with latest swift 2.1):

func imageWithImage (sourceImage:UIImage, scaledToWidth: CGFloat) -> UIImage {      let oldWidth = sourceImage.size.width      let scaleFactor = scaledToWidth / oldWidth        let newHeight = sourceImage.size.height * scaleFactor      let newWidth = oldWidth * scaleFactor        UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))      sourceImage.drawInRect(CGRectMake(0, 0, newWidth, newHeight))      let newImage = UIGraphicsGetImageFromCurrentImageContext()      UIGraphicsEndImageContext()      return newImage  }  

Answer by Suraj Sukale for Resize UIImage by keeping Aspect ratio and width


Set the frame of your UIImageView and Set the contentMode to resizing options:

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


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

Related Posts:

0 comments:

Post a Comment

Popular Posts

Fun Page

Powered by Blogger.