xcode 以编程方式阻止 UIImageView 自动调整大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7792664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically prevent UIImageView from autoresizing
提问by Eric Brotto
I would like to truncate one end of a UIImageView by changing the size of the frame dynamically. Of course, if it autoresizes it won't look 'truncated', but will look shrunk. The former is the effect I would like, the latter not.
我想通过动态更改框架的大小来截断 UIImageView 的一端。当然,如果它自动调整大小,它看起来不会“被截断”,而是会缩小。前者是我想要的效果,后者不是。
So far I've set up the UIImageView in IB and then hooked it up programmatically using IBOutlet. I then placed this in ViewDidLoad, but still get the autoresizing:
到目前为止,我已经在 IB 中设置了 UIImageView,然后使用 IBOutlet 以编程方式将其连接起来。然后我把它放在 ViewDidLoad 中,但仍然可以自动调整大小:
[self.stringImageView setAutoresizingMask:UIViewAutoresizingNone];
self.stringImageView.frame = CGRectMake(9.0f, 508.0f, 500.0f, 26.0f);
Any ideas how I can clip my UIImageView instead of having it resize?
任何想法如何剪辑我的 UIImageView 而不是调整它的大小?
EDIT 1
编辑 1
It should also be noted that in IB I have deselected Clip Subviews
and Autoresize Subviews
.
还应该注意的是,在 IB 中,我取消了选择Clip Subviews
和Autoresize Subviews
。
采纳答案by Saurabh Passolia
You need to clip the UIImage object first according to imageview's frame and then set it as image for image view. See also Cropping an UIImage
您需要先根据imageview的frame裁剪UIImage对象,然后将其设置为image进行image view。另请参阅裁剪 UIImage
回答by CedricSoubrie
Use : stringImageView.contentMode = UIViewContentModeCenter;
so the image you add to the UIImageView always keeps the same size and the same position in your view.
使用 :stringImageView.contentMode = UIViewContentModeCenter;
因此您添加到 UIImageView 的图像在您的视图中始终保持相同的大小和相同的位置。
回答by bioffe
@implementation UIImage (Resize)
// Returns a copy of this image that is cropped to the given bounds.
// The bounds will be adjusted using CGRectIntegral.
// This method ignores the image's imageOrientation setting.
- (UIImage *)croppedImage:(CGRect)bounds {
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
回答by Maffo
In Interface Builder set Mode in the View Tab to "Top" or whatever it should be.
在 Interface Builder 中,将 View 选项卡中的 Mode 设置为“Top”或任何它应该是的。
回答by Hamid
Another more efficient way to go if you only wish to make part of the image visible, or for example animate its appearance,
如果您只想使图像的一部分可见,或者例如为其外观设置动画,则另一种更有效的方法是,
is to set the content mode of the UIImageView
to UIViewContentModeCenter
(or any other alignment) and include it in a UIScrollView
.
是设置UIImageView
to UIViewContentModeCenter
(或任何其他对齐方式)的内容模式并将其包含在UIScrollView
.
You will only need to change the frame of the scroll view and your image will be visually cropped without you having to create the cropped image (which would be a very high price if it is for animation purpose).
您只需要更改滚动视图的框架,您的图像将在视觉上被裁剪,而无需创建裁剪的图像(如果用于动画目的,这将是一个非常高的价格)。