简体   繁体   English

放置完整尺寸的图像以适合CALayer的整个屏幕

[英]Place a full sized image to fit the entire screen in a CALayer

I have an image (png) which must fill the entire screen of my app. 我有一张图片(png),该图片必须填满我应用的整个屏幕。 I'm using CALayers and doing everything programatically but still this sounds like something that should be trivial to but I can't get it to work. 我正在使用CALayers并以编程方式进行所有操作,但这仍然听起来有些琐碎,但我无法使其正常工作。 I have two versions of the image a retina version (2048px x 1536px) and a non-retina version 1024px x 768px). 我有两个版本的图像:视网膜版本(2048px x 1536px)和非视网膜版本1024px x 768px)。 The image is listed a universal image in the Asset catalogue 该图像在资产目录中列出为通用图像

The code is simple enough I think: 我认为代码很简单:

// CREATE FULL SCREEN CALAYER  
CALayer *myLayer = [[CALayer alloc] init];
[myLayer setBounds:CGRectMake(0, 0, bounds.size.width, bounds.size.height)]; 
[myLayer setPosition:CGPointMake(bounds.size.width/2, bounds.size.height/2)];
[self.view.layer addSublayer:myLayer];

// LOAD THE IMAGE INTO THE LAYER —— AM EXPECTING IT TO FILL THE LAYER
UIImage *layerImage = [UIImage imageNamed:@"infoScreen"];
CGImageRef image = [layerImage CGImage];
[myLayer setContents:(__bridge id)image];
[myLayer setContentsGravity:kCAGravityCenter]; /* IT WORKS FINE IF I USE setContentsGravity:kCAGravityResizeAspectFill */

This code works fine on a non-iPad retina. 此代码可在非iPad视网膜上正常工作。 However on the Retina iPad, the image is always loaded at twice the actual size (so it appears zoomed in). 但是,在Retina iPad上,图像始终以实际大小的两倍加载(因此显示为放大)。 I'm using the Simulator and iOS 8. What am I doing wrong? 我正在使用模拟器和iOS8。我在做什么错?

Beging your image processing with func UIGraphicsBeginImageContextWithOptions(size: CGSize, opaque: Bool, scale: CGFloat) 使用功能UIGraphicsBeginImageContextWithOptions(大小:CGSize,不透明:布尔,比例:CGFloat)来开始图像处理

The last parameter in the above function determines the scaling for the graphics. 上面函数中的最后一个参数确定图形的缩放比例。 You can set this value by retrieving the scale property of the main screen. 您可以通过检索主屏幕的scale属性来设置此值。 In swift I would do it this way: var screen = UIScreen.mainScreen() var scale = screen.scale 很快我就可以这样:var screen = UIScreen.mainScreen()var scale = screen.scale

Hope it helps. 希望能帮助到你。

Edit: - Code for doing this in swift, you can modify it to suit your need. 编辑:-快速执行此操作的代码,您可以对其进行修改以适合您的需要。

    UIGraphicsBeginImageContextWithOptions(rect.size, true, 0.0)
    var ctx : CGContextRef = UIGraphicsGetCurrentContext()
    <UIImage>.drawInRect(rect)

I had this same problem, was solved by setting the contentsScale value on the CALayer - for some reason the default scale on CALayers is always 1.0, even on Retina devices. 我遇到了同样的问题,可以通过在CALayer上设置contentsScale值来解决-由于某些原因,即使在Retina设备上,CALayers的默认缩放比例也始终为1.0。

ie

layer.contentsScale = [UIScreen mainScreen].scale;

Also, if you're drawing a shape using CAShapeLayer and wondering its edges look a little jagged on retina devices, try: 另外,如果您要使用CAShapeLayer绘制形状,并且想知道其边缘在视网膜设备上看起来有点锯齿,请尝试:

shapeLayer.rasterizationScale = [UIScreen mainScreen].scale;
shapeLayer.shouldRasterize = YES;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM