In my iphone app i need to set image in the image view.
the image view height is always 100pixels.
we need to set the width of the image view with respect to the original image scale ratio.
ie per suppose width X height of an image is 300x400 (original) (4x3)
our displayed image view image size would be 75X100
width X height of an image is 512x512 (original) (1x1)
our displayed image view image size would be 100X100
width X height of an image is 128 x 256 (original) (1x2)
our displayed image view image size would be 50X100
in all those cases image view height 100pixel should be same, but the width need to varie with respect to the image ratio.
And finally the image view should be in center of the view
How to achieve it
There are a couple of ways you can achieve this. The simplest is to set the scaling mode on the image view to Aspect Fill in Interface Builder, or
imageView.contentMode = UIViewContentModeScaleAspectFit;
The second way is to calculate the aspect ratio of the image and set the width. Something like:
UIImage *myImage = [UIImage imageNamed:@"MyImage"];
float aspectRatio = myImage.size.width / myImage.size.height;
float viewWidth = 100 * aspectRatio;
Well Its up to your logics.
You can get UIImage
height and width.
UIImage *image = [UIImage imageNamed:@"anyImage.png"];
float imageHeight = image.size.height;
float imageWidth = image.size.width;
After that you can calculate imageViewWidth-.
According to your requirement -
float value = imageHeight/100.0;
float iV_Width = imageWidth/value;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.