Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView content mode

enter image description here

The blue line is the imageview's bounds. UIImageView's contentMode is UIViewContentModeScaleAspectFit, And I want keep the original picture's scale. How can I make the picture's left edge is on the UIImageView's left edge? But not like UIViewContentModeTopLeft mode. Keep the scale and no blank in the left. My english is not good, hope you guys could understand what I'm saying. Thank you very much.

like image 910
jxdwinter Avatar asked Oct 16 '14 09:10

jxdwinter


People also ask

What is content mode?

The content mode specifies how the cached bitmap of the view's layer is adjusted when the view's bounds change. This property is often used to implement resizable controls.

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

What is use of UIImageView?

Whenever you want an image in a view controller, you use a UIImageView. A UIImageView contains a UIImage, which is the raw bitmap, and allows you to configure how you want to scale or crop the image.

What is UIImageView in Swift?

A view that displays a single image or a sequence of animated images in your interface.


1 Answers

 yourimageView.contentMode = UIViewContentModeCenter;
if ( yourimageView.bounds.size.width > yourimageView.size.width && yourimageView.bounds.size.height > yourimageView.size.height) {
   yourimageView.contentMode = UIViewContentModeScaleAspectFit;
}

Swift3

yourimageView.contentMode = .center
if yourimageView.bounds.size.width > yourimageView.size.width && yourimageView.bounds.size.height > yourimageView.size.height {
yourimageView.contentMode = .scaleAspectFit
}

you can change your mode as

typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};
like image 146
Anbu.Karthik Avatar answered Oct 02 '22 20:10

Anbu.Karthik