在 CALayer 中添加 UIImage

add UIImage in CALayer(在 CALayer 中添加 UIImage)
本文介绍了在 CALayer 中添加 UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是代码:

- (void)viewDidLoad{//......CALayer *layer = [CALayer 层];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);}别的{layer.bounds = CGRectMake(self.mapView.frame.origin.x,self.mapView.frame.origin.y, 150, 700);}layer.contents = (id)[UIImage imageNamed:@"myImage.png"];//名称正确但在输出中图像不可见[[self.mapView 层] addSublayer:layer];[图层设置需要显示];}

解决方案

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

如何将 UIImage 添加到 CALayer

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

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

  • 注意UIImage需要转换成CGImage.

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

让 myLayer = CALayer()让 myImage = UIImage(named: "star")?.cgImagemyLayer.frame = myView.boundsmyLayer.contents = myImagemyView.layer.addSublayer(myLayer)

修改外观

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

不过,正如您所见,它看起来像素化了.这是因为 UIImageUIView 小,因此它被缩放以填充视图,这是默认设置,您无需指定任何其他内容.

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

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

在 iOS 中,您可能需要设置

kCAGravityResizeAspect

kCAGravityResizeAspectFill

kCAGravityCenter

kCAGravityTop

kCAGravityBottom

kCAGravityLeft

kCAGravityRight

kCAGravityTopLeft

kCAGravityTopRight

kCAGravityBottomLeft

kCAGravityBottomRight

相关

  • 视图的内容模式属性
  • 使用 CGContextDrawImagedrawRect 中绘制 UIImage>
  • CALayer 教程:入门

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.

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];
}

解决方案

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

You can add an image to a view's layer simply by using its contents property:

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

  • Note that the UIImage needs to be converted to a 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 UIView and the dark blue star is the UIImage.

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

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

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

In iOS, you may want to set the isGeometryFlipped property to true if 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.)

There are two UIView examples below for every contentsGravity setting, one view is larger than the UIImage and the other is smaller. This way you can see the effects of the scaling and gravity.

kCAGravityResize

This is the default.

kCAGravityResizeAspect

kCAGravityResizeAspectFill

kCAGravityCenter

kCAGravityTop

kCAGravityBottom

kCAGravityLeft

kCAGravityRight

kCAGravityTopLeft

kCAGravityTopRight

kCAGravityBottomLeft

kCAGravityBottomRight

Related

  • Content mode property of a view
  • Drawing a UIImage in drawRect with CGContextDrawImage
  • CALayer Tutorial: Getting Started

这篇关于在 CALayer 中添加 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)