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;
}
How to resize UIImageView based on UIImage's size/ratio in Swift 3?
It looks like you want to resize an ImageView according to the image ratio and the container view's size, here is the example in Swift (Sorry,the former answer with a bug, I fixed it):
let containerView = UIView(frame: CGRect(x:0,y:0,width:320,height:500))
let imageView = UIImageView()
if let image = UIImage(named: "a_image") {
let ratio = image.size.width / image.size.height
if containerView.frame.width > containerView.frame.height {
let newHeight = containerView.frame.width / ratio
imageView.frame.size = CGSize(width: containerView.frame.width, height: newHeight)
}
else{
let newWidth = containerView.frame.height * ratio
imageView.frame.size = CGSize(width: newWidth, height: containerView.frame.height)
}
}
How to resize UIImage programmatically to be like setting scaleAspectFill content mode?
I have used this for one of my projects.
extension UIImage
{
func imageWithSize(size:CGSize) -> UIImage
{
var scaledImageRect = CGRect.zero
let aspectWidth:CGFloat = size.width / self.size.width
let aspectHeight:CGFloat = size.height / self.size.height
//max - scaleAspectFill | min - scaleAspectFit
let aspectRatio:CGFloat = max(aspectWidth, aspectHeight)
scaledImageRect.size.width = self.size.width * aspectRatio
scaledImageRect.size.height = self.size.height * aspectRatio
scaledImageRect.origin.x = (size.width - scaledImageRect.size.width) / 2.0
scaledImageRect.origin.y = (size.height - scaledImageRect.size.height) / 2.0
UIGraphicsBeginImageContextWithOptions(size, false, 0)
self.draw(in: scaledImageRect)
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage!
}
}
Resize UIImage with aspect ratio?
After you set your screen rect, do something like the following to decide what rect to draw the image in:
float hfactor = value.bounds.size.width / screenRect.size.width;
float vfactor = value.bounds.size.height / screenRect.size.height;
float factor = fmax(hfactor, vfactor);
// Divide the size by the greater of the vertical or horizontal shrinkage factor
float newWidth = value.bounds.size.width / factor;
float newHeight = value.bounds.size.height / factor;
// Then figure out if you need to offset it to center vertically or horizontally
float leftOffset = (screenRect.size.width - newWidth) / 2;
float topOffset = (screenRect.size.height - newHeight) / 2;
CGRect newRect = CGRectMake(leftOffset, topOffset, newWidth, newHeight);
If you don't want to enlarge images smaller than the screenRect, make sure factor
is greater than or equal to one (e.g. factor = fmax(factor, 1)
).
To get the black background, you would probably just want to set the context color to black and call fillRect before drawing the image.
The simplest way to resize an UIImage?
The simplest way is to set the frame of your UIImageView
and set the contentMode
to one of the resizing options.
Or you can use this utility method, if you actually need to resize an image:
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Example usage:
#import "MYUtil.h"
…
UIImage *myIcon = [MYUtil imageWithImage:myUIImageInstance scaledToSize:CGSizeMake(20, 20)];
UIImage resize aspect fill and crop
Try this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
imagenUsr = [info objectForKey:UIImagePickerControllerOriginalImage];
double width = imagenUsr.size.width;
double height = imagenUsr.size.height;
double screenWidth = self.view.frame.size.width;
double apect = width/height;
double nHeight = screenWidth/ apect;
self.imgView.frame = CGRectMake(0, 0, screenWidth, nHeight);
self.imgView.center = self.view.center;
self.imgView.image = imagenUsr;
}
This will help you keeping the aspect ratio as before.
Another way would be:
self.imgView.contentMode = UIViewContentModeScaleAspectFit;
self.imgView.image = imagenUsr;
Hope this helps.. :)
Related Topics
Using Nsuserdefaults on Arrays
How to Upload Images to a Server in iOS With Swift
How to Iterate For Loop in Reverse Order in Swift
How to Make Uilabel Display Outlined Text
Changing Specific Text's Color Using Nsmutableattributedstring in Swift
Why Nsdateformatter Is Returning Null for a 19/10/2014 in a Brazilian Time Zone
iOS Uitextview or Uilabel with Clickable Links to Actions
How to Resize the Iphone/iPad Simulator
Passing Data Between View Controllers Using Segue
Close iOS Keyboard by Touching Anywhere Using Swift
How to Animate the Background Color of a Uilabel
How to Load Gif Image in Swift
Ios: Multi-Line Uilabel in Auto Layout
iPhone Sdk: Differencebetween Loadview and Viewdidload
Disable Gesture to Pull Down Form/Page Sheet Modal Presentation