2011-03-24 80 views
1

我一直在努力/搜索幾天纔得到這個工作無濟於事。我不確定我錯過了什麼。CAGradientLayer當renderInContext正在消失

我創建一個「卡」前視圖並添加到UIView圖層。在屏幕上,一切看起來都很棒,當調用renderInContext時,一層不會顯示。

編輯:想補充只有「突出顯示」漸變圖層不工作其他漸變圖層按預期工作。

- (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; 

和代碼來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]; 
+0

如果locations數組包含的值超出[0,1],則不會使用renderInContext進行渲染。 – 2012-12-03 23:21:51

回答

4

感謝張貼你的代碼,這使得它更容易幫助:)

一對夫婦的誤解......

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

這條線是錯誤的。 [CALayer layer]返回完全形成的圖層,調用init可能會爲圖層做一些「有趣」的事情。通常,當你調用一個返回新實例的類方法時(除了alloc),你不應該調用init。

此代碼下面的這個調用將導致崩潰(如果它曾經被執行過,見下文)。

[cardFrontContainer release] 

由於layer沒有在名稱中「複製」,「新」或「頁頭」的方法,它返回一個對象,你不應該釋放。該圖層返回'autoreleased',因此不會被您保留。

請閱讀ObjC內存管理指南here

獲取更多信息。

此外,你應該聽編譯器,當它告訴你那些釋放調用無法到達時,它意味着它。 return之後什麼都不會執行。

一般來說,除非它們非常小(少於幾十千字節),否則不應該將圖像放入數據庫中。看看here

有關該主題的更多信息。

至於爲什麼不繪製漸變,它可能是不同的顏色空間,但這似乎不是問題。

我會說試試清理至少雙重init問題和內存問題,然後再試一次。

+0

感謝您的反饋意見。我已經根據你的建議進行了更新,但就我的問題而言,它沒有什麼不同,但是,可能會導致一些問題。正在存儲的圖像非常小,爲23kb。謝謝 – downed 2011-03-24 20:27:23

2

出現,幸運的是,我能夠通過簡單地更改我的CAGradientLayer顏色數組的值來解決我的問題。

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

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]; 

我會假設[的UIColor whiteColor]和RGBACOLOR(255,255,255,1.0)基本上是相同的,renderInContext並不這麼認爲。

+0

這是問題,如上所述,不同的顏色空間(我應該注意到...)[UIColor whiteColor]和[UIColor blackColor]在灰色空間中 – 2011-03-24 20:52:52