IOS image filling UIImageView (contentMode)
This article mainly introduces the contentMode attribute of UIView:
UIViewContentModeScaleAspectFit, // This image will be displayed in the view, and the proportion remains unchanged. That is to say, if the ratio of the image to the view is different, it will be left blank as 1
UIViewContentModeScaleAspectFill, // This indicates that the entire view will be filled with images, and the image proportion will remain unchanged, so that the image display will be larger than view 2
Since the requirements are not high and cannot be left blank, I can use the second method, but this is beyond the position, so my colleagues can cut it out again.
Then the complete process will take two steps.
[Self. prp_imageViewsetContentMode: UIViewContentModeScaleAspectFill];
Self. prp_imageView.clipsToBounds = YES;
Perfect solution:
I really like them so much that I should be my model for the moment.
Then I tried all the tests. Let's take a look at the picture to summarize the differences.
UIViewContentModeCenter
UIViewContentModeTop
UIViewContentModeBottom
UIViewContentModeLeft
UIViewContentModeRight
UIViewContentModeTopLeft
UIViewContentModeTopRight
UIViewContentModeBottomLeft
UIViewContentModeBottomRight
Other more detailed attributes:
UIView has a contentMode attribute of the UIViewContentMode type, which can be used to modify the content display mode of the view.
View sourceprint?01.
typedef NS_ENUM(NSInteger, UIViewContentMode) {
02.
UIViewContentModeScaleToFill,
03.
UIViewContentModeScaleAspectFit,
// contents scaled to fit with fixed aspect. remainder is transparent
04.
UIViewContentModeScaleAspectFill,
// contents scaled to fill with fixed aspect. some portion of content may be clipped.
05.
UIViewContentModeRedraw,
// redraw on bounds change (calls -setNeedsDisplay)
06.
UIViewContentModeCenter,
// contents remain same size. positioned adjusted.
07.
UIViewContentModeTop,
08.
UIViewContentModeBottom,
09.
UIViewContentModeLeft,
10.
UIViewContentModeRight,
11.
UIViewContentModeTopLeft,
12.
UIViewContentModeTopRight,
13.
UIViewContentModeBottomLeft,
14.
UIViewContentModeBottomRight,
15.
};
Instance code:
View sourceprint?1.
CGRect rect = self.view.frame;
2.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect];
3.
imageView.contentMode = UIViewContentModeTop;
4.
imageView.image = [UIImage imageNamed:@
demoImage
];
5.
[self.view addSubview:imageView];
UIViewContentModeScaleToFill
Stretch the image content according to the proportion of the view.
UIViewContentModeScaleAspectFit
Maintain the aspect ratio of the image content to adapt to the view size.
UIViewContentModeScaleAspectFill
Fill in the View Size with the image content. The excess part can be trimmed to fill the entire view boundary.
UIViewContentModeRedraw
This option is re-displayed by calling the setNeedsDisplay method when the size and position of a single view change.
UIViewContentModeCenter
Keep the original image proportion in the middle of the view to display the image content
If the view size is smaller than the image size, the image exceeds the view boundary.
UIViewContentModeTop
Keep the original proportion of the image displayed on the top of the middle of the View
UIViewContentModeBottom
Keep the original image proportion in the middle of the view to show the image content
UIViewContentModeLeft
Keep the original image proportion shown on the left of the View
UIViewContentModeRight
Keep the original image proportion shown on the right of the View
UIViewContentModeTopLeft
Keep the original image proportion in the upper left corner of the view to display the image content.
UIViewContentModeTopRight
Keep the original image proportion in the upper right corner of the view to display the image content
UIViewContentModeBottomLeft
Keep the original image proportion shown in the lower left corner of the View
UIViewContentModeBottomRight
Keep the original image proportion in the lower right corner of the view.