简体   繁体   English

当renderInContext时CAGradientLayer消失

[英]CAGradientLayer disappearing when renderInContext

I've been trying/searching for a few days to get this to work to no avail. 我一直在尝试/搜索几天,以使其无法正常工作。 I'm not sure what I'm missing. 我不确定我缺少什么。

I'm creating a "card" front view and adding to UIView layer. 我正在创建“卡”前视图,并将其添加到UIView层。 On screen everything looks great, when calling renderInContext one layer will not display. 在屏幕上,一切看起来都很不错,调用renderInContext时将不会显示一层。

EDIT: Wanted to add that only the "highlight" gradient layer is not working the other gradient layer is working as expected. 编辑:想补充一点,只有“突出显示”渐变层无法正常工作,另一个渐变层却可以正常工作。

- (CALayer *)createCardFront {

NSLog(@"Card createCardFront Called");

// Create the layers we want to use

CALayer *cardFrontContainer = [CALayer layer];
CALayer *processedImage = [CALayer layer];
CAGradientLayer *gradient = [CAGradientLayer layer];
CAGradientLayer *hightlight = [CAGradientLayer layer];
CALayer *icon = [CALayer layer];
CAShapeLayer *gloss = [CAShapeLayer layer];

// Position them correctly

[cardFrontContainer addSublayer:processedImage];
[cardFrontContainer addSublayer:gradient];
[cardFrontContainer addSublayer:hightlight];
[cardFrontContainer addSublayer:icon];
[cardFrontContainer addSublayer:gloss];


// Base size for card

CGRect rect = CGRectMake(0., 0., (200. * [cardScale floatValue]), (276. * [cardScale floatValue]));

// Container Properties

cardFrontContainer.bounds = rect;
cardFrontContainer.position = CGPointMake((frontView.center.x * [cardScale floatValue]), (138. * [cardScale floatValue])-10);
cardFrontContainer.cornerRadius = (25. * [cardScale floatValue]);
cardFrontContainer.masksToBounds = YES;
cardFrontContainer.borderWidth = .5;
cardFrontContainer.borderColor = [RGBACOLOR(120,120,120, .7) CGColor];
[cardFrontContainer setNeedsDisplay];

// Highlight Properites

hightlight.bounds = rect;
hightlight.position = CGPointMake((100. * [cardScale floatValue]), (138. * [cardScale floatValue]));
hightlight.startPoint = CGPointMake(0., 0.0);
hightlight.endPoint = CGPointMake(0., 1.);
hightlight.opacity = 1.;
hightlight.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:.0111],
                        [NSNumber numberWithFloat:.0999],
                        [NSNumber numberWithFloat:1.], nil];

hightlight.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor],
                     (id)[RGBACOLOR(255,255,255, .0) CGColor],
                     (id)[RGBACOLOR(255,255,255, .0) CGColor], nil];


// Gradient Properites

gradient.bounds = rect;
gradient.position = CGPointMake((100. * [cardScale floatValue]), (138. * [cardScale floatValue]));
gradient.startPoint = CGPointMake(0, 0.5);
gradient.endPoint = CGPointMake(1.0, 0.5);
gradient.opacity = 1.;
gradient.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:.0111],
                            [NSNumber numberWithFloat:.099],
                            [NSNumber numberWithFloat:.899],
                            [NSNumber numberWithFloat:.9999], nil];

gradient.colors = [NSArray arrayWithObjects:(id)[RGBACOLOR(0,0,0, .3) CGColor],
                         (id)[[UIColor clearColor] CGColor],
                         (id)[[UIColor clearColor] CGColor],
                         (id)[RGBACOLOR(0,0,0, .3) CGColor], nil];



// Background Properites

processedImage.bounds = rect;
processedImage.position = CGPointMake((100. * [cardScale floatValue]), (138. * [cardScale floatValue]));
processedImage.contents = (id)[cachedBackground CGImage];


// Icon Properites

iconImage = [[self processThumbnailImageForLayer] retain];

icon.bounds = CGRectMake(0., 0., (200 * [cardScale floatValue]), (276 * [cardScale floatValue]));
icon.position = CGPointMake((100. * [cardScale floatValue]), (138. * [cardScale floatValue]));
icon.contents = (id)[iconImage CGImage];



// Set the Gloss Layer

gloss.frame = CGRectMake(0., 0., (200. * [cardScale floatValue]), (200. * [cardScale floatValue]));
gloss.position = CGPointMake(0.,0.);
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath,NULL,(100. * [cardScale floatValue]),(350. * [cardScale floatValue]));
CGPathAddLineToPoint(thePath, NULL, (100. * [cardScale floatValue]), (100. * [cardScale floatValue]));
CGPathAddLineToPoint(thePath, NULL, (270. * [cardScale floatValue]), (100. * [cardScale floatValue]));
CGPathAddCurveToPoint(thePath,NULL,
                      (200. * [cardScale floatValue]),(140. * [cardScale floatValue]),
                      (100. * [cardScale floatValue]),(245. * [cardScale floatValue]),
                      (100. * [cardScale floatValue]),(376. * [cardScale floatValue]));
CGPathCloseSubpath(thePath);
gloss.path = thePath;
CGPathRelease(thePath);
gloss.opacity = .3;
gloss.fillColor = [[UIColor whiteColor] CGColor];


return cardFrontContainer;

And the code to renderInContext: 以及renderInContext的代码:

- (void) imageFromView {

NSLog(@"Card imageFromView Called");

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];



UIGraphicsBeginImageContext(CGSizeMake(170., 238.));
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *imageData = UIImagePNGRepresentation (viewImage);

// Add to core data object

attributeObject.cardCache = imageData;
attributeObject.shouldUpdateCard = NO;

[self saveAction];

[self.managedObjectContext reset];
 self.managedObjectContext = nil;

[pool release];

Thanks for posting your code, it makes it much easier to help :) 感谢您发布代码,它使帮助变得更加容易:)

A couple of misunderstandings... 几个误会...

CALayer *cardFrontContainer = [[CALayer layer] init];

This line is wrong. 这行是错误的。 [CALayer layer] returns a fully formed layer, calling init is probably doing some 'interesting' things to the layer. [CALayer layer]返回一个完整的层,调用init可能正在对该层做一些“有趣的”事情。 In general when you call a class method that returns a new instance (apart from alloc ) you should never call init. 通常,当您调用返回新实例(除alloc )的类方法时,切勿调用init。

Also this call below in the code will lead to a crash (if it were ever executed, see below). 同样,代码下面的此调用也会导致崩溃(如果曾经执行过,请参见下文)。

[cardFrontContainer release]

Since the method layer does not have 'copy', 'new' or 'alloc' in the name it is returning an object that you should not release. 由于方法layer中没有“ copy”,“ new”或“ alloc”,因此返回的是您不应释放的对象。 The layer is returned 'autoreleased' and is thus not retained by you. 该图层将返回“自动释放”,因此您不保留该图层。

Please read the ObjC memory management guide here 请在此处阅读ObjC内存管理指南

for more info. 有关更多信息。

Also, you should listen to the compiler, when it tells you those release calls can't be reached, it means it. 同样,您应该听编译器的消息,当编译器告诉您无法到达那些释放调用时,就意味着可以了。 After the return nothing will be executed. return之后,将不执行任何操作。

In general you should never put image's into the database, unless they are really small (less than a few dozen kilobytes). 通常,除非图像真的很小(小于几十千字节),否则永远不要将图像放入数据库中。 Take a look here 在这里看看

for more info on that topic. 有关该主题的更多信息。

As to why it's not drawing the gradient, it could be different color spaces, but that does not appear to be the problem. 至于为什么不绘制渐变,可能是不同的色彩空间,但这似乎不是问题。

I'd say try cleaning up at least the double init issue and the memory problems then try again. 我想说的是至少清理双重init问题和内存问题,然后再试一次。

Appears, by luck, I was able to get my problem solved by simply changing the values of my CAGradientLayer colors array. 幸运的是,似乎可以通过简单地更改CAGradientLayer colors数组的值来解决问题。

hightlight.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor],
                 (id)[RGBACOLOR(255,255,255, .0) CGColor],
                 (id)[RGBACOLOR(255,255,255, .0) CGColor], nil];

to

hightlight.colors = [NSArray arrayWithObjects:(id)[RGBACOLOR(255,255,255, 1.0) CGColor],
                 (id)[RGBACOLOR(255,255,255, .0) CGColor],
                 (id)[RGBACOLOR(255,255,255, .0) CGColor], nil];

I would have assumed that [UIColor whiteColor] and RGBACOLOR(255,255,255, 1.0) are essentially the same, renderInContext doesn't think so. 我会假设[UIColor whiteColor]和RGBACOLOR(255,255,255,1.0)本质上是相同的,renderInContext并不这么认为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM