I need to set the UIImageView size aspectfit according to height .
means my requirement is that my height of image should be fix but the width of image should be change according to height i know that in UIViewContentModeScaleAspectFit it will take width and hieght automatically but what if i need fix height and dynamic width like aspectfit according to height.
UIImage *originalImage = [UIImage imageNamed:#"image.png"];
double width = originalImage.size.width;
double height = originalImage.size.height;
double apect = width/height;
double nWidth = 320.f/ apect;
self.img.frame = CGRectMake(0, 0, nWidth, 320.f);
self.img.center = self.view.center;
self.img.image = originalImage;
-(CGSize)resizeImage:(CGSize)imageSize toTargetedHeight:(float)height {
NSLog(#"actualImageSize : %#", NSStringFromCGSize(imageSize));
float scaleFactor = height / imageSize.height;
float newHeight = imageSize.height * scaleFactor;
float newWidth = imageSize.width * scaleFactor;
CGSize newSize = CGSizeMake(newWidth, newHeight);
NSLog(#"convertedImageSize : %#", NSStringFromCGSize(newSize));
return newSize;
}
This will create your image with specific height with maintaining its aspect ratio.
Related
I am trying to do this in storyboard but can't seem to figure it out. I have a QR code (square UIImageView) centered (vertically and horizontally) in another UIView that I want to expand a bit depending on phone size, but I don't want it to go over 150x150 otherwise it looks odd. Here is what I tried. Any help greatly appreciated.
You want to:
Center it in its superview
Keep 1:1 aspect ratio
Keep it less-than-or-equal-to 150x150
So, centering is obvious (yay, auto layout). 1:1 ratio is also easy. The Trick is:
Pin all 4 sides to the superview sides at >= 0.
Set the Width <= 150.
And then ---
Set another Width constraint, this time Width Equals: 150 but with Priority of less-than 1000. I used 999 and it did the job.
Pinning the sides and top/bottom with >= 0 gets it to shrink when the superview is smaller than 150 (in either direction).
I have been using this for a long time.
(UIImage*) scaleImage:(UIImage*)image toSize:(CGSize)newSize {
CGSize scaledSize = newSize;
float scaleFactor = 1.0;
if( image.size.width > image.size.height ) {
scaleFactor = image.size.width / image.size.height;
scaledSize.width = newSize.width;
scaledSize.height = newSize.height / scaleFactor;
}
else {
scaleFactor = image.size.height / image.size.width;
scaledSize.height = newSize.height;
scaledSize.width = newSize.width / scaleFactor;
}
UIGraphicsBeginImageContextWithOptions( scaledSize, NO, 0.0 );
CGRect scaledImageRect = CGRectMake( 0.0, 0.0, scaledSize.width,
scaledSize.height );
[image drawInRect:scaledImageRect];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
I need to resize UIImage proportionally by width, may be someone have a working code?
For ex.:
Input: Width: 900 Height: 675. Resize to width: 400 width
Result: Width: 400 Height: 300
Please help..
Check this blog post out. In particular, these two files:
UIImage+Resize.h
UIImage+Resize.m
Maybe this would work for you:
UIImage *image = [UIImage imageNamed:#"SomeImage-900x675"]; // SomeImage-900x675.png
CGFloat targetWidth = 400.0f;
CGFloat scaleFactor = targetWidth / image.size.width;
CGFloat targetHeight = image.size.height * scaleFactor;
CGSize targetSize = CGSizeMake(targetWidth, targetHeight);
UIImage *scaledImage = [image resizedImage:targetSize interpolationQuality:kCGInterpolationHigh];
Couldn't you just set the content mode? to .ScaleAspectFill? Then you can set the UIImageView size to any arbitrary size and the aspect ratio will be preserved.
https://developer.apple.com/reference/uikit/uiviewcontentmode/1622518-scaleaspectfill
I have a UIImage and I want to adjust what is shown on the device based on a UIImageView.
The UIImage is:
UIImage *image = // a PNG I have
// width = 1200
CGFloat width = image.size.width;
// height = 900
CGFloat height = image.size.height;
And I have an UIImageView
UIImageView *iview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 675.0, 900.0)];
How do I get my UIImage into the UIImageView without adjust the aspect ratio? I want the UIImage to be cropped off the edges?
hope this helps
-(UIImage*)crop:(CGRect)frame
{
// Find the scalefactors UIImageView's widht and height / UIImage width and height
CGFloat widthScale = self.bounds.size.width / self.image.size.width;
CGFloat heightScale = self.bounds.size.height / self.image.size.height;
// Calculate the right crop rectangle
frame.origin.x = frame.origin.x * (1 / widthScale);
frame.origin.y = frame.origin.y * (1 / heightScale);
frame.size.width = frame.size.width * (1 / widthScale);
frame.size.height = frame.size.height * (1 / heightScale);
// Create a new UIImage
CGImageRef imageRef = CGImageCreateWithImageInRect(self.image.CGImage, frame);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;//(return value is uiimage)
}
and then add it to uiimageview
If u want to crop off the edges and center the position of ur image then
Set the Imageview properties to AspectFill
myImageView.contentMode = UIViewContentModeScaleAspectFill;
myImageView.clipsToBounds = YES;
Hope this helps
Try setting the contentMode property of your UIImageView to UIViewContentModeScaleAspectFill, which according to the documentation is:
The option to scale the content to fill the size of the view. Some portion of the
content may be clipped to fill the view’s bounds.
You can see the other options in the UIView documentation.
How do I resize UIImageView after setting its contentmode to UIViewContentModeScaleAspectFit so that i can remove the white spaces from top & below.
Please see the attached image
Thanks in advance:)
Hope this will help some others as well ,please do comment if it will break in some condition :)
- (CGRect)frameForImageattribute:(CGSize)image inImageViewAspectFit:(UIImageView *)imageView {
float imageRatio = image.width / image.height;
float viewRatio = imageView.frame.size.width / imageView.frame.size.height;
if (imageRatio < viewRatio) {
float scale = imageView.frame.size.height / image.height;
float width = scale * image.width;
return CGRectMake(kLeftPading, kTopPading, width, imageView.frame.size.height);
}
else {
float scale = imageView.frame.size.width / image.width;
float height = scale * image.height;
return CGRectMake(kLeftPading, kTopPading, imageView.frame.size.width, height);
}
}
You need to get the scale factor of the image view. it can be obtained by
float scaleFactor = MAX(image.size.width/imageView.bounds.size.width, image.size.height/imageView.bounds.size.height);
Then do
CGRect ivFrame = imageView.frame;
ivframe.size.height = image.size.height/scalefactor;
ivFrame.size.width = image.size.width/scalefactor;
imageView.frame = ivFrame;
theres probably a category out there that does this automagically.
EDIT: Heres one for the scalefactor calculation, and it even respects the content mode of the mageView:
how can I get the scale factor of a UIImageView who's mode is AspectFit?
If you know only the width of the imageview and when the height of the image is dynamic then you need to scale the image's height according to the given width to remove the white spaces above and below your image in UIViewContentModeScaleAspectFit mode. Use the following method from here to scale the height of the image according to the standard width of your screen.
-(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;
}
And call it from your cellForRowAtIndexPath: method like this:
UIImage *img = [dictImages objectForKey:yourImageKey]; // loaded the image
cell.imgView.image = [self imageWithImage:img scaledToWidth:self.view.frame.size.width];
I've an UIImageView with content mode Aspect Fit of size 220x155. I'm dynamically inserting different images in different resolutions, but all larger than the size of the UIImageView. As the content mode is set to Aspect Fit, the image is scaled with respect to the ratio to fit the UIImageView.
My problem is, that if for instance the image inside the UIImageView is scaled to 220x100, I would like the UIImageView to shrink from a height of 155 to 100 too to avoid space between my elements.
How can I do this?
If I got you right, it would be something like this: get image size by:
UIImage * img = [UIImage imageNamed:#"someImage.png"];
CGSize imgSize = img.size;
calculate scale ratio on width
float ratio=yourImageView.frame.size.width/imgSize.width;
check scaled height (using same ratio to keep aspect)
float scaledHeight=imgSize.height*ratio;
if(scaledHeight < yourImageView.frame.size.height)
{
//update height of your imageView frame with scaledHeight
}
Based on Michael's answer, here's a complete method
+ (CGSize)makeSize:(CGSize)originalSize fitInSize:(CGSize)boxSize
{
float widthScale = 0;
float heightScale = 0;
widthScale = boxSize.width/originalSize.width;
heightScale = boxSize.height/originalSize.height;
float scale = MIN(widthScale, heightScale);
CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);
return newSize;
}
Edit for Steven Stefanik's answer: This method breaks when originalSize is {0, 0}. Maybe consider these changes
-(CGSize)makeSize:(CGSize)originalSize fitInSize:(CGSize)boxSize
{
if (originalSize.height == 0) {
originalSize.height = boxSize.height;
}
if (originalSize.width == 0) {
originalSize.width = boxSize.width;
}
float widthScale = 0;
float heightScale = 0;
widthScale = boxSize.width/originalSize.width;
heightScale = boxSize.height/originalSize.height;
float scale = MIN(widthScale, heightScale);
CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);
return newSize;
}
Last edit:
It seems I misunderstood the question.
To get the actual size you could try:
CGSize imageSize = img.size;
CGSize viewSize = imageView.frame.size;
CGSize actualSize;
if (imageSize.width > imageSize.height) {
actualSize.width = imageSize.width > viewSize.width ? viewSize.width : imageSize.width;
actualSize.height = imageSize.height * actualSize.width / imageSize.width;
}
else {
actualSize.height = imageSize.height > viewSize.height ? viewSize.height : imageSize.height;
actualSize.width = imageSize.width * actualSize.height / imageSize.height;
}