UIImage *img = [[UIImage alloc] init]; [img setImage:[UIImage imageNamed:@"anyImageName"]];
UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .
An object that manages image data in your app.
An object that manages image-based content and allows you to perform animations on that content.
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;
or in a Swift playground (you will have to provide your own PNG image in the Playground's resource file. I'm using the example of "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
}
ARC version requires a different cast:
self.myView.layer.contents = (__bridge id) self.myImage.CGImage;
CALayer *layer = [[CALayer alloc] init];
layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"REWIFISocketOff"].CGImage);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With