How to resize a image by it's width and Maintain Aspect Ratio - iphone

I want to resize or scale a image down by it's width is fixed. I have been searching all over the network, it's about resizing the image to new size by specifying new CGSize value. I only want to scale it down by its width is fixed, like 100px, and the height scale down and Maintain it's Aspect Ratio.

check this out and modify as per your needs.
http://code.developwithus.com/iphone/resize-image-in-iphone-sdk/

Related

how to get device pixel ratio using flutter

as you might know there are two types of size in flutter app (physical,logical).
for margin and padding if we want the app to be responsive we must get context size (width,height).
for a reason I don't want to use MediaQuery.of(context).size so I'm using
Size screenSize = WidgetsBinding.instance.window.physicalSize;
double width = screenSize.width;
double height = screenSize.height;
in such case the size is physical and can't be used like before for padding and margin with edgeinsets.
what I'm asking is a way to either change physical pixel size to logical without using context and MediaQuery or a way to use physical pixels to give responsive margin and padding.
thanks in advance.
I tried to get the ratio with testing results it differs between devices so we can't use static ratio.
The ratio is not the ratio of height/width of physical size.
You can get the screen size without context like this:
var size = MediaQueryData.fromWindow(WidgetsBinding.instance.window).size;
now if you print this size, you can see it is equals to the size that you get from MediaQuery with context.

Add an image to the map and make sure aspect ratio is correct

I have tried adding an image to Leaflet using ImageOverlay, see for example the following code:
imageBounds = [[40.712216, -74.22655], [40.773941, -74.12544]];
var ol = L.imageOverlay(imageUrl, imageBounds).addTo(mymap);
Now the image is added, but I would like to use the aspect ratio of the image and I would prefer to actually set the image's center point instead of the bounds. Is this at all possible?
Size of the image is not that critical, lets say I want the width to be set to 60% of the current visible area of the map, I then want the height to be calculated so that the aspect ratio of the image is kept.
Problem is I am not sure I can just translate between latitude/longitude and x/y.
Is it correct to assume that the aspect ratio longitude/latitude is the same as for x/y if the map is quite zoomed in?
Is there an easier way to do this maybe?

Incorrect UITableViewCell height UIImageView

I have a UITableViewCell that only contains a UIImageView. I am using UITableViewAutomaticDimension which is working perfectly for all the other cells in my tableview, except for this image view cell.
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
I want the image to be as tall as it wants, maintain the image's aspect ratio and fill the width. To do that I set the image view's constraints to be pinned to the cell's frame using auto layout and I set the image views contentMode to be Aspect Fit. I set the UIImageView's background to be red. And the cell above it is white. The actual image is black down below.
When I do that, the cell has about 100 px above the image and 100px below. but the images size is exactly what I want. Has anyone had the same issue?
Because that red space isn't always the same size and it was a lot taller when I have a really big image in there, I believe the layout thinks the height should be the actual height of the image, without any aspect fit constraint.
Thanks for any help!
I want the image to be as tall as it wants, maintain the image's aspect ratio and fill the width. To do that I set the image view's constraints to be pinned to the cell's frame using auto layout and I set the image views contentMode to be Aspect Fit.
You can't do all that with only auto layout. This is because a UIImageView's intrinsic content size is just its image's full size.
Instead, calculate the appropriate height and set it when you set your image:
let imageViewWidth : CGFloat = // the width you want…
imageHeightConstraint.constant = image.size.height / image.size.width * imageViewWidth;
Note that this will fail if you have a 0x0 image due to division by zero. If that's possible in your app, check for this case and just set the height constant to 0.
At this point, reload the row so that the table view will recalculate the height.

Scaling while conserving ration

Upon using the convert method, I would like to be able to transform a landscape or portrait image given the height and width specify without altering the ratio.
From the documentation, the 'clip' options act as follow:
'clip': Resizes the image to fit within the specified parameters without distorting, cropping, or changing the aspect ratio
If I have a 200x50 image and I want a 150x150 result, this would result in a 150x37px resized image with its ratio identical to the original's.
If I have a 100x50 image and I want a 150x150 result, this would result in a 150x75px resized image with its ratio identical to the original's.
'crop': Resizes the image to fit the specified parameters exactly by removing any parts of the image that don't fit within the
boundaries
If I have a 200x50 image and I want a 150x150 result, this would result in a 150x37px cropped image.
'scale': Resizes the image to fit the specified parameters exactly by scaling the image to the desired size
If I have a 200x50 image and I want a 150x150 result, this would result in a 150x150px resized image where the ratio has been altered to fit.
'max': Resizes the image to fit within the parameters, but as opposed to 'clip' will not scale the image if the image is smaller
than the output size
Same output as in 'clip' except that if I have a 100x50 image and I want a 150x150 result, this would result in a 100x50px resized image with its ratio identical to the original's.
What I would like to have is the ability to make an image conserve its ratio and be of the required dimension (with vertical and horizontal centering if need be). It would result in an image that is not distorted nor clipped.
I understand there are some trickiness to the task as you have to determine what color do you fill the space with (see ImageMagick doc about space filling).
Any insight would be great, hope it is not too much of an edge case.
Take a look at this set of examples in the ImageMagick documentation:
http://www.imagemagick.org/Usage/thumbnails/#square
We don't currently offer the ability to "fill" empty parts of the image with a background color, so do not support this use case. We are looking at adding it in the near term, and will update you when this is added.

Difference between stretching and scaling an image

Can anybody please tell me what is the exact difference between stretching and scaling an image? Because you can anyway set the size of image and imageView both to match your requirements.
It depends on how you define stretching, but I would divide scaling into two distinct options based on whether or not the aspect ratio is preserved. Often it is desired to preserve the aspect ratio when scaling an image.
I would consider an increase in one dimension, but not proportionally in the other to be a "stretch". Similarly, a decrease in one dimension, but not proportionally in the other would be a "squash".
You may find this Daring Fireball post interesting.
Stretching sounds like showing small size (10x10) image at (100x100) or (100x10). so some times it gets pix-elated.
And scaling means to show a image to different size either small or big with maintaining its aspect ratio (programmetically), so it will look not improper, because when you stretch to different aspect ratio then some objects in image gets improper visibility.
Stretching (in iphone IB) means '9-slice scaling', scaling means just scaling.
When stretching you can determine which part of the image may be used for stretching and which part may not. For example when you have a rounded square, you do not want the roundings to stretch, especially when you're only stretching horizontally or vertically.
You indicate that you only want to use the middle pixel to stretch by (in IB) setting the X & Y values to 0.50 (half way) and the width & height values to 0.00 (minimum amount of pixels)
Lookup contentStretch in the docs for more info
when you don,t keep the congruence of your image, you see the image incongruous and height and width of your image is not suitable for showing. for resolving this issue you can multiply your image's width and height to to a constant coefficient.
Stretching and scaling don't mean anything different except maybe in connotation.
Is there a particular piece of text somewhere that you are trying to understand? Maybe we can help with that.
stretching image is stretching the size of a small image.
on the other hand scaling of image is scaling the image accoring the the viewport's width and viewport's height....
scaling can be done by small as well as large image.
you should take a good quality image and then should scale it
sprite.setscale(x,y);