2011-03-24 9 views
1

나는이 기능을 사용하지 않으려면 며칠 동안 노력/검색 해왔다. 내가 뭘 놓쳤는 지 모르겠다.renderInContext가 사라질 때 CAGradientLayer가 사라짐

나는 "카드"정면도를 만들고 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를 사용하여 렌더링하지 않습니다. 피드백에 대해 –

답변

4

감사합니다, 그것은 ... 훨씬 쉽게 오해의 :

몇 수 있도록 할 수 있습니다

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

이 줄이 잘못되었습니다. [CALayer layer]은 완전하게 형성된 레이어를 반환하고, init을 호출하면 레이어에 '흥미로운'일을하고있을 것입니다. 일반적으로 (alloc을 제외하고) 새 인스턴스를 반환하는 클래스 메서드를 호출하면 절대로 init을 호출하면 안됩니다.

아래 코드를 호출하면 충돌이 발생할 수 있습니다 (실행 한 경우 아래 참조).

[cardFrontContainer release] 

는 이름에 '복사', '새'또는 'ALLOC을'이없는 layer 방법은 당신이 해제해서는 안 객체를 반환하기 때문에. 레이어는 'autoreleased'로 반환되므로 사용자가 보유하지 않습니다.

는 더 많은 정보를 위해 ObjC 메모리 관리 가이드 here

을 읽어 보시기 바랍니다.

또한 릴리스 호출에 도달 할 수 없다는 것을 알려주는 컴파일러를 청취해야합니다. return 이후 아무 것도 실행되지 않습니다.

일반적으로 이미지는 데이터베이스에 저장하지 않아야합니다 (실제로는 수십 킬로바이트 미만). 확인해보십시오 here

자세한 내용은 해당 정보를 참조하십시오.

그라데이션을 그리지 않는 이유는 다른 색 공간 일 수 있지만 문제는 아닙니다.

적어도 이중 init 문제를 해결하고 메모리 문제를 다시 시도해보십시오.

+0

주셔서 감사합니다. 귀하의 제안에 따라 업데이트했습니다. 문제가 해결되는 한 다른 문제는 발생하지 않지만 일부 문제가 발생했을 수 있습니다. 저장되는 이미지는 23kb로 매우 작습니다. 위에 언급 한대로 문제가되는 – downed

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]는 회색 색상 공간에 있습니다. –

관련 문제