ios 插入 UIImageView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32304349/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:27:33  来源:igfitidea点击:

Insets to UIImageView?

iosiphoneuiimageviewstoryboard

提问by Sarasranglt

I would like to achieve :

我想实现:

  • Scale to fill mode of image view
  • Insets to UIImageViewso that image does not meet the edges of the UIImageView
  • 缩放到图像视图的填充模式
  • 插入以UIImageView使图像不与图像的边缘相交UIImageView

Bottomline: I want to increase the touch area of the UIImageViewwithout stretching the image beyond certain size.

底线:我想在UIImageView不将图像拉伸到超过一定尺寸的情况下增加触摸区域。

Is it possible through storyboard ? If not can anyone tell how to do it programmatically ?

可以通过故事板吗?如果没有谁能告诉如何以编程方式做到这一点?

I can achieve similar functionality through a UIButtonand setting image inset through storyboard but I can't find any such thing with UIImageView.

我可以通过 aUIButton并通过故事板设置图像插入来实现类似的功能,但我找不到任何这样的东西UIImageView.

采纳答案by Sarasranglt

Using the method given here:

使用这里给出的方法:

UIImage *image= [MyUtil imageWithImage:[UIImage imageNamed:@"MyImage"] scaledToSize:CGSizeMake(80, 80)];

UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
imgView.contentMode = UIViewContentModeCenter;
[imgView setImage:image];

Now your insets become 100-80 i.e. 20 .

现在您的插图变为 100-80 ,即 20 。

This is a workaround to provide insets to an UIImageView. Better answers are welcomed.

这是一种向 UIImageView 提供插图的解决方法。欢迎更好的答案。

回答by BumMo Koo

You can add inset to UIImageinstead of UIImageView.

您可以添加 insetUIImage而不是UIImageView

Swift 4

斯威夫特 4

let imageView = UIImageView() 
imageView.contentMode = .scaleAspectFill
imageView.image = UIImage(named: "example")?.withAlignmentRectInsets(UIEdgeInsets(top: -4, left: 0, bottom: -4, right: 0))

回答by MarkHim

It looks like you want to have a touchable imageView, so why not using an UIButton with image and imageEdgeInsets?

看起来你想要一个可触摸的 imageView,那么为什么不使用带有 image 和 imageEdgeInsets 的 UIButton 呢?

UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setFrame:someFrame];
[b setImage:img forState:UIControlStateNormal];
b.imageEdgeInsets = UIEdgeInsetsMake(20, 20, 20, 20);
[b addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];

回答by shtnkgm

My Code

我的代码

  • minus inset: smaller image
  • plus inset: larger image
  • 减去插图:较小的图像
  • 加插图:更大的图像

Swift4

斯威夫特4

private lazy var imageView: UIImageView = {
    let view = UIImageView()
    view.contentMode = .scaleAspectFit
    let insetValue: CGFloat = -8
    view.image = image.withAlignmentRectInsets(UIEdgeInsets(top: insetValue, left: insetValue, bottom: insetValue, right: insetValue))
    return view
}()

回答by Nikolay Derkach

One option is to use withAlignmentRectInsets, but it doesn't work with the layer border.

一种选择是使用withAlignmentRectInsets,但它不适用于图层边框。

Another way is to use resizableImage(withCapInsets: resizingMode: .stretch)where you'd want to use positive inset values for smaller images.

另一种方法是使用resizableImage(withCapInsets: resizingMode: .stretch)您希望为较小图像使用正插入值的地方。

Sample code:

示例代码:

private func setupQRImageView() -> UIImageView {
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFit
    imageView.layer.borderColor = Default.imageBorderColor.cgColor
    imageView.layer.borderWidth = 4.0
    imageView.layer.cornerRadius = 6.0
    let imageInset: CGFloat = 8.0
    imageView.image = UIImage(named: "QR")?.resizableImage(withCapInsets: UIEdgeInsets(top: imageInset, left: imageInset, bottom: imageInset, right: imageInset), resizingMode: .stretch)
    return imageView
}

Produces the following result:

产生以下结果:

UIImageView with a border and padded inset

带有边框和填充插图的 UIImageView

回答by Saheb Roy

You can add a UIViewfirst and then you add your UIImageViewinside the UIViewwith a padding of whatever you like, and if you want your gesture to get affected only in UIImageView, make it UserInteractionEnable to yes or if you want the gesture to be in the whole then you can add the gesture to the whole UIView

您可以添加UIView,然后再添加你的UIImageViewUIView用任何你喜欢的填充,如果你希望你的姿态,只有在得到受影响UIImageView,使其UserInteractionEnable为yes或者,如果你想的手势是整个然后你可以将手势添加到整体UIView

回答by Ionz

Try work around with UIImage within UIView like this ;

尝试像这样在 UIView 中使用 UIImage ;

enter image description here

enter image description here