1

我正在使用 MKNetwrkKit。我有一个imageView,我已将它作为一个添加subView到一个UIView. 我使用"UIImageView+MKNetworkKitAdditions.h". 在下载完成之前,我添加了一个掩码ImageView并将其从superView中删除,然后我崩溃了!

- (void) testUIImageViewDownloadCrash {
    UIImageView *imageView = [[UIImageView alloc] init];

    [imageView mk_setImageAtURL:[NSURL URLWithString:@"http://www.gravatar.com/avatar/a717291747c76567bb0f086e15ae6e43?s=32&d=identicon&r=PG"] onCompletion:^(BOOL success, BOOL fromCache) {
        //
    }];

    UIView *view = [[UIView alloc] init];
    [view addSubview:imageView];
    [self addMaskToImageView:imageView];
    [imageView removeFromSuperview];
}

现在,奇怪的是,只有当我添加掩码时才会发生这种崩溃......这是添加掩码的函数。

- (CAGradientLayer*) addMaskToImageView:(UIImageView *) inImageView
{
    CALayer *imageLayer = inImageView.layer;
    CGRect imageBounds = imageLayer.bounds;
    CGRect ellipseRect = CGRectInset(imageBounds, 4, 4);

    CGPathRef maskPath = CGPathCreateWithEllipseInRect(ellipseRect, NULL);
    if (maskPath)
    {
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.bounds = imageBounds;
        maskLayer.position = (CGPoint){CGRectGetMidX(imageBounds), CGRectGetMidY(imageBounds)};
        maskLayer.path = maskPath;
        maskLayer.fillColor = [UIColor blackColor].CGColor;

        imageLayer.mask = maskLayer;
        CFRelease(maskPath);        
    }

    CAGradientLayer *gradientRingLayer = [self createGradientRing:imageBounds];

    CALayer *containerLayer = [CALayer layer];

    [imageLayer.superlayer addSublayer:containerLayer];
    // Move container layer to the image position
    containerLayer.position = imageLayer.position;
    // Image is now at 0,0 in the container
    imageLayer.position = CGPointZero;

    [containerLayer addSublayer:gradientRingLayer];
    [containerLayer addSublayer:imageLayer];
         layer.shouldRasterize = YES;
         layer.rasterizationScale = [[UIScreen mainScreen] scale];
         layer.contentsScale = [[UIScreen mainScreen] scale];


    containerLayer.shadowColor = [UIColor blackColor].CGColor;
    containerLayer.shadowOpacity = 1.0f;
    containerLayer.shadowOffset = CGSizeMake(2.0f, 5.0f);
    containerLayer.shadowRadius = 7.5f;
    // Create shadow at the righ place around the image
    CGPathRef containerShadowPath = CGPathCreateWithEllipseInRect(imageLayer.frame, NULL);
    containerLayer.shadowPath = containerShadowPath;
    CFRelease(containerShadowPath);

    return gradientRingLayer;
}

- (CAGradientLayer*)createGradientRing:(CGRect)inBounds {
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.anchorPoint = CGPointMake(0.5f, 0.5f);
        gradient.position = CGPointMake(0.0f, 0.0f);
        //    gradient.startPoint = CGPointMake(0.0,0.0);
        //    gradient.endPoint = CGPointMake(0.0,0.0);
        gradient.bounds = inBounds;
        gradient.cornerRadius = 0.0;
        gradient.colors = [NSArray arrayWithObjects:
                           (id)[UIColor ringLight].CGColor,
                           (id)[UIColor ringLight].CGColor,
                           nil];

        CAShapeLayer *frameLayer = [CAShapeLayer layer];
        CGPathRef framePath = CGPathCreateWithEllipseInRect(CGRectInset(inBounds, 0, 0), NULL);
        if (framePath)
        {
            frameLayer.bounds = inBounds;
            frameLayer.position = (CGPoint){CGRectGetMidX(inBounds), CGRectGetMidY(inBounds)};
            frameLayer.path = framePath;
            frameLayer.fillColor = [UIColor blackColor].CGColor;
            CFRelease(framePath);
        }

        gradient.mask = frameLayer;

        return gradient;
    }

如果我注释掉添加掩码,它不会崩溃。我不明白的另一件事是,添加掩码后,如果这样做:

NSLog(@"subviews count : %i",[view.subviews count]);

然后我得到0!去哪儿了imageView

我观察到该addMask函数正在生成一个superViewimageView值。我发现这条线[containerLayer addSublayer:imageLayer];是罪魁祸首。这是使superViewnull 的imageView。但为什么?我该如何解决?

有时我也会遇到这个异常,我不知道为什么。

-[__NSCFString sublayers]: unrecognized selector sent to instance

我在 iOS 5 和 iOS 6 上试过。

4

0 回答 0