CALayer从UIImagePicker加载图像时出现故障

时间:2013-05-08 19:19:41

标签: ios xcode4 uiimageview calayer layer

我有一个带有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];

并在顶层添加子图层只是花花公子。 我错过了一些明显的东西吗?会喜欢任何建议。 感谢。

1 个答案:

答案 0 :(得分:0)

因为

holderLayer和containerLayer 透明 (holderLayer.opacity = 0 / containerLayer.opacity = 0)

制作它,

 //container layer - the very top layer
CALayer *containerLayer = [CALayer layer];
containerLayer.opacity = 1.0;
containerLayer.bounds = [self.imageView.layer frame];

// Holder Layer
CALayer *holderLayer = [CALayer layer];
holderLayer.opacity = 1.0;
holderLayer.bounds = self.imageView.bounds;