我有一个带有 UIImageView (imageView) 的 UIViewController,并且我在 viewDidLoad 中定义了几个将嵌套在 imageview 中的层,如下所示:
//container layer - the very top layer
CALayer *containerLayer = [CALayer layer];
containerLayer.opacity = 0;
containerLayer.bounds = [self.imageView.layer frame];
// Holder Layer
CALayer *holderLayer = [CALayer layer];
holderLayer.opacity = 0;
holderLayer.bounds = self.imageView.bounds;
// Hierarchy layers
[containerLayer setValue:holderLayer forKey:@"__holderLayer"];
[containerLayer addSublayer:holderLayer];
[self.imageView.layer addSublayer:containerLayer];
从 UIImagePicker 加载图像时,我有以下代码有效:
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageView *newImgView = [[UIImageView alloc] initWithFrame:self.imageView.frame];
CGRect frame = self.imageView.frame;
newImgView.image = image;
frame.origin = CGPointMake(0, 0);
newImgView.layer.frame = frame;
newImgView.layer.opacity = .9;
newImgView.layer.contentsGravity = kCAGravityResizeAspectFill;
CALayer * containerLayer = self.imageView.layer.sublayers[0];
if (containerLayer != nil)
{
[containerLayer setValue:newImgView.layer forKey:@"__imageLayer"];
CALayer * holderLayer = [containerLayer valueForKey:@"__holderLayer"];
if (holderLayer != nil)
{
//!!!!!line below doesn't work!!!
//[holderLayer addSublayer:newImgView.layer];
//line below works!
[self.imageView.layer addSublayer:newImgView.layer];
}
}
[self.imageView setNeedsDisplay];
[self checkAndPrintLayers];
所以第一个嵌套层是containerLayer,然后是holderLayer,我期望将各种图像作为子层添加到holderLayer,然后对其进行操作。然而,调用
[holderLayer addSublayer:newImgView.layer];
不工作;imageView 保持空白。然而,调用
[self.imageView.layer addSublayer:newImgView.layer];
并在顶层添加一个子层只是花花公子。我在这里遗漏了一些明显的东西吗?希望有任何建议。谢谢。