ios 在 CALayer 中添加 UIImage

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

add UIImage in CALayer

iosuiimageviewuiimagecalayersubview

提问by Adriana Carelli

I must add a UIImageView as subview of MapView. To do this I created a layer above the MapView. In this layer I want to put my image, but I get a white rectangle and nothing else. My image is not visible.

我必须添加一个 UIImageView 作为 MapView 的子视图。为此,我在 MapView 上方创建了一个图层。在这一层我想把我的形象,但我得到一个白色的矩形,没有别的。我的图像不可见。

This is the code:

这是代码:

- (void)viewDidLoad
{
    //......

    CALayer *layer = [CALayer layer];
    layer.backgroundColor = [[UIColor whiteColor] CGColor];

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        layer.bounds = CGRectMake(self.mapView.bounds.origin.x,
                                  self.mapView.bounds.origin.y, 80, 300);
    }
    else
    {
        layer.bounds = CGRectMake(self.mapView.frame.origin.x,
                                  self.mapView.frame.origin.y, 150, 700);
    }

    layer.contents = (id)[UIImage imageNamed:@"myImage.png"];
    //the name is correct but  in the output the image is not visible

    [[self.mapView layer] addSublayer:layer];
    [layer setNeedsDisplay];
}

回答by Suragch

This is a general answer for the sake of future viewers. It is based on the question title rather than the details of the original question.

为了未来的观众,这是一个通用的答案。它基于问题标题而不是原始问题的详细信息。

How to add a UIImage to a CALayer

如何将 UIImage 添加到 CALayer

You can add an image to a view's layersimply by using its contentsproperty:

您可以layer简单地使用其contents属性将图像添加到视图:

myView.layer.contents = UIImage(named: "star")?.cgImage
  • Note that the UIImageneeds to be converted to a CGImage.
  • 请注意,UIImage需要转换为CGImage.

If you wish to add the image in its own layer, you can do it like this:

如果你想在自己的图层中添加图像,你可以这样做:

let myLayer = CALayer()
let myImage = UIImage(named: "star")?.cgImage
myLayer.frame = myView.bounds
myLayer.contents = myImage
myView.layer.addSublayer(myLayer)

Modifying the appearance

修改外观

The above code produces a view like this. The light blue is the UIViewand the dark blue star is the UIImage.

上面的代码产生了这样的视图。浅蓝色是UIView,深蓝色星是UIImage

enter image description here

在此处输入图片说明

As you can see, though, it looks pixelated. This is because the UIImageis smaller than the UIViewso it is being scaled to fill the view, which is the default it you don't specify anything else.

但是,正如您所看到的,它看起来是像素化的。这是因为UIImage小于 ,UIView所以它被缩放以填充视图,这是默认值,您没有指定任何其他内容。

The examples below show variations on the layer's contentsGravityproperty. The code looks like this:

下面的示例显示了图层contentsGravity属性的变化。代码如下所示:

myView.layer.contents = UIImage(named: "star")?.cgImage
myView.layer.contentsGravity = kCAGravityTop
myView.layer.isGeometryFlipped = true

In iOS, you may want to set the isGeometryFlippedpropertyto trueif you are doing anything with top or bottom gravity, otherwise it will be the opposite of what you expect. (Only the gravity is flipped vertically, not the content rendering. If you are having trouble with the content being flipped, see this answer.)

在iOS中,您可能需要设置isGeometryFlipped属性true,如果你正在做的顶部或底部重力什么,否则这将是你所期望的相反。(只有重力垂直翻转,而不是内容渲染。如果您在翻转内容时遇到问题,请参阅此答案。)

There are two UIViewexamples below for every contentsGravitysetting, one view is larger than the UIImageand the other is smaller. This way you can see the effects of the scaling and gravity.

UIView每个contentsGravity设置下面有两个示例,一个视图大于另一个视图UIImage。通过这种方式,您可以看到缩放和重力的效果。

kCAGravityResize

kCAGravityResize

This is the default.

这是默认设置。

enter image description here

在此处输入图片说明

kCAGravityResizeAspect

kCAGravityResizeAspect

enter image description here

在此处输入图片说明

kCAGravityResizeAspectFill

kCAGravityResizeAspectFill

enter image description here

在此处输入图片说明

kCAGravityCenter

kCAGravityCenter

enter image description here

在此处输入图片说明

kCAGravityTop

kCAGravityTop

enter image description here

在此处输入图片说明

kCAGravityBottom

kCAGravityBottom

enter image description here

在此处输入图片说明

kCAGravityLeft

kCAGravityLeft

enter image description here

在此处输入图片说明

kCAGravityRight

kCAGravityRight

enter image description here

在此处输入图片说明

kCAGravityTopLeft

kCAGravityTopLeft

enter image description here

在此处输入图片说明

kCAGravityTopRight

kCAGravityTopRight

enter image description here

在此处输入图片说明

kCAGravityBottomLeft

kCAGravityBottomLeft

enter image description here

在此处输入图片说明

kCAGravityBottomRight

kCAGravityBottomRight

enter image description here

在此处输入图片说明

Related

有关的

回答by Bastian

it has to be

它一定要是

layer.contents = (id)[UIImage imageNamed:@"myImage.png"].CGImage;

You can only put a CGImage into a layer, not an UIImage directly.

CGImage 只能放入图层,不能直接放入 UIImage。

回答by Adriana Carelli

I removed

我删除了

 [layer setNeedsDisplay];

I do not know why, but it works!

我不知道为什么,但它有效!

回答by Soul Clinic

You need to set the frame of the CALayer properly, because the default is CGRectZero.

需要正确设置CALayer的frame,因为默认是CGRectZero。

回答by JonyFang

myView.layer.contents = UIImage(named: "star")?.cgImage

myView.layer.contents = UIImage(named: "star")?.cgImage