UIImage* backgroundImage = [UIImage imageNamed:kBackName];
CALayer* aLayer = [CALayer layer];
CGFloat nativeWidth = CGImageGetWidth(backgroundImage.CGImage);
CGFloat nativeHeight = CGImageGetHeight(backgroundImage.CGImage);
CGRect startFrame = CGRectMake(0.0, 0.0, nativeWidth, nativeHeight);
aLayer.contents = (id)backgroundImage.CGImage;
aLayer.frame = startFrame;
o en un patio de recreo Swift (que tendrá que proporcionar su propia imagen PNG en el archivo de recursos de la Zona de juegos. Estoy usando el ejemplo de "FrogAvatar".)
//: Playground - noun: a place where people can play
import UIKit
if let backgroundImage = UIImage(named: "FrogAvatar") // you will have to provide your own image in your playground's Resource file
{
let height = backgroundImage.size.height
let width = backgroundImage.size.width
let aLayer = CALayer()
let startFrame = CGRect(x: 0, y: 0, width: width, height: height)
let aView = UIView(frame: startFrame)
aLayer.frame = startFrame
aLayer.contentsScale = aView.contentScaleFactor
aLayer.contents = backgroundImage.cgImage
aView.layer.addSublayer(aLayer)
aView // look at this via the Playground's eye icon
}
![An image as the content of a CALayer embedded in a UIView](https://i.stack.imgur.com/tBWnG.png)
¿Dónde has leído esto? UIViews en general son envoltorios livianos alrededor de CALayers, y no he visto mucha diferencia entre ellos en cuanto a la memoria o el rendimiento de la pantalla. Tampoco estoy seguro de si compro las 3 copias de una imagen en el argumento de la memoria. En cualquier caso, los CALayers son sin duda parte de Core Animation (por lo tanto, el prefijo de CA). –