We all know that in iOS, every uiimageview has his frame size, but what happens if the size of the picture doesn't match the size of the frame? By default, the picture is compressed or stretched to fill the entire area.
By looking at the properties of the UIView, you can see that the Contentmode property of the view can be used to control how the picture is displayed. The following settings allow the picture to be centered.
1 |
imageView.contentMode = UIViewContentModeCenter; |
This centering is included, both horizontal and vertical are centered. The picture does not stretch or compress, which is centered on the size of the frame and picture of the ImageView.
Here are two things:
1, the picture is larger than the view area. At this point, the middle part of the image is captured in the frame area.
2, the picture is smaller than the view area. At this point the picture is displayed in the middle of the frame in its entirety.
If, in the default case, the extra portion of the picture is still displayed on the screen. If you do not want the area above the frame to be displayed on the screen to be set. The Clipstobounds property.
1 |
imageView.clipsToBounds = YES; |
The last question, above the retina screen of the iphone, must be set to the Contentscalefactor property. The default value for this property is 1. The two corresponding retina screens need to be 2. You can set it in the following ways:
1 |
[imageView setContentScaleFactor:[[UIScreen mainScreen] scale]]; |
But with:
1 |
imageView.contentMode = UIViewContentModeCenter; |
There are also his problems, that is, when the picture is irregular, and the width or height of the picture is smaller than the width of frame, there will be a blank situation.
In order to resolve this problem you can set:
1 |
imageView.contentMode = UIViewContentModeScaleAspectFill; |
So the picture will be stretched or compressed to fit the frame boundary, but also to adapt to a smaller edge, so that the effect is that the picture to adapt to the smallest side of the display, the larger side will go beyond the frame, if the Clipstobounds property is set to Yes, then the larger edge will be truncated. This way to achieve a better center display effect, the complete code is as follows:
1 |
UIImage *pic = [ UIImage imageNamed:@"IMG_0404.PNG"]; |
2 |
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 240, 100 )]; |
3 |
[imageView setImage:pic]; |
4 |
[imageView setContentScaleFactor:[[UIScreen mainScreen] scale]]; |
5 |
imageView.contentMode = UIViewContentModeScaleAspectFill; |
6 |
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight; |
7 |
imageView.clipsToBounds = YES; |
Original link Address: http://www.ganlvji.com/?p=139
About the display problem of Uiimageview--centering or capturing the middle part of the picture shows