CoreInimation与UIImage

我正在尝试使用PNG图像在Xcode制作一个Core Animation。 到目前为止,我得到了使用CGRectMake绘制一个小方块,它的工作负载CGRectMake屏幕的框。 现在我要用一个layer.contents = [UIImage imageNamed:@""];replaceCGRectlayer.contents = [UIImage imageNamed:@""]; types线。 任何帮助? 谢谢。

(工作代码的其余部分:))

。H

 #import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> @interface ViewController : UIViewController { CAShapeLayer * layer; UIImage * new; //hypothetical image } @end 

.M

 #import "ViewController.h" @interface ViewController () @end @implementation ViewController //[self view] - (void)viewDidLoad { //layer layer = [CAShapeLayer layer]; layer.position = CGPointMake ([self view].bounds.size.width /2, [self view].bounds.size.height /2 ); layer.bounds= CGRectMake (0,0,100,100); layer.borderWidth = 2; layer.borderColor = [[UIColor colorWithRed:0 green:0 blue:0 alpha:1 ] CGColor] ; [[[self view] layer] addSublayer: layer]; CABasicAnimation * moveAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"]; moveAnimation.duration = 1; //starting point for the animation moveAnimation.fromValue = [NSNumber numberWithFloat:layer.position.y]; //end point for the animation moveAnimation.toValue = [NSNumber numberWithFloat:layer.position.y + 100]; //this line makes the animation stay to its position after moving layer.position = CGPointMake(layer.position.x, layer.position.y +100); [layer addAnimation:moveAnimation forKey: @"move1"]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. 

}

 - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end 

最后添加行后,它的工作

 UIImage *image2 = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pic@2x" ofType:@"png"]]; 

在图层上方以及指定build议的新娘演员。 下面是did load方法的最终代码。

 - (void)viewDidLoad { UIImage *image2 = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pic@2x" ofType:@"png"]]; layer = [CALayer layer]; layer.contents = (__bridge id)image2.CGImage; layer.position = CGPointMake ([self view].bounds.size.width /2, [self view].bounds.size.height /3 ); layer.bounds= CGRectMake (100,100,500,500); [[[self view] layer] addSublayer: layer]; CABasicAnimation * moveAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"]; moveAnimation.duration = 1; moveAnimation.fromValue = [NSNumber numberWithFloat:layer.position.y]; moveAnimation.toValue = [NSNumber numberWithFloat:layer.position.y + 100]; layer.position = CGPointMake(layer.position.x, layer.position.y +100); [layer addAnimation:moveAnimation forKey: @"move1"]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. 

}

你可以像这样将一个图层的内容设置为一个UIImage:

 layer.contents = (__bridge id)image.CGImage; 

一个图层为其内容提供一个CGImageRef (核心graphics图像引用) – 这是为了允许OS X和iOS之间的兼容性。 因此,您需要传入UIImage对象的CGImage属性,该对象是上图中的image 。 OS X上的NSImage具有相同的属性。

但是,CGImageRef不是NSObject ,而是核心基础types。 (__bridge id)是一个桥接types ,您可以使用它来使编译器将其视为NSObject 。 您可以通过search桥接模式或免费桥接来了解更多关于这些原则的信息,但这不在本答案的范围之内。

你可能应该这样做到基地CALayer而不是一个形状层。 形状图层希望从您提供的path中创build自己的内容。