2012-05-31 2 views
0

CGlayerRef와 CGcontextRef를 NSdata에 저장했는데 현재 NSlayer에 CGlayerRef와 CGcontextRef를 저장했는데 현재의 NSlayer를 덮어 쓰게되면 Pointer가 저장된 것이지만 데이터는 저장되지 않는다는 것을 알았습니다. 그런 이유로이 CGlayerRef 또는 CGcontextRef를 복사본으로 만들 수 없습니다 (drawRect에서이 작업을 모두 수행했습니다).CGlayerRef 또는 CGcontextRef를 NSdata에 저장할 수없는 이유는 무엇입니까?

왜 일부 NSData는 이러한 내용을 저장할 수 있지만 NSvalue는 작업 할 수는 없지만 작동하지는 않습니다. 전혀, 나는 "사본"을 사용해도 이러한 데이터의 사본을 얻지 못했습니다.

- (무효)의 drawRect : 여기

내 코드입니다 (CGRect) RECT {

경우 (drawCount == 3) {

 dataLayer1=[NSData dataWithBytes:& _layerView 

길이는 sizeof (CGLayerRef)]

[dataLayer1 retain]; 

       } 



    if (undodo==YES) { 

     _layerView=*(CGLayerRef*)[dataLayer1 bytes]; 



    } 



    currentContext = UIGraphicsGetCurrentContext(.... 

답변

0

비트 맵 컨텍스트에서 CGLayerRef를 만들고 비트 맵 컨텍스트에 버퍼를 제공해야합니다. 그런 다음 레이어로 그릴 때 비트 맵 컨텍스트 백업 버퍼에 새 데이터가 있어야합니다.

0

CGLayer는 NSValue로 저장할 수 있습니다. 당신이 그것의 일부에 CGLayer에 연속 된 도면을 가지고 싶다면,해야 NSValue에 CGLayer

- (CGLayerRef)copyLayer:(CGLayerRef)layer 
{ 
    CGSize size = CGLayerGetSize(layer); 
    UIGraphicsBeginImageContext(size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGLayerRef copyLayer = CGLayerCreateWithContext(context, size, NULL); 
    CGContextRef copyContext = CGLayerGetContext(copyLayer); 
    CGContextDrawLayerInRect(copyContext, CGRectMake(0, 0, size.width, size.height), layer); 
    UIGraphicsEndImageContext(); 

    return copyLayer; 
} 

2.CGLayer 1.copy

& & NSValue CGLayer

NSValue

-
//CGLayer to NSValue 
CGLayerRef copyLayer = [self copyLayer:theLayerYouWantToSave]; 
NSValue *layerValue = [NSValue valueWithLayer:copyLayer]; 

//NSValue to CGLayer 
theLayerYouWantToRestore = [layerValue layerValue]; 

3.add 카테고리 0

//NSValue category 
+ (NSValue *)valueWithLayer:(CGLayerRef)layer 
{ 
    NSValue *value = [NSValue value:&layer withObjCType:@encode(CGLayerRef)]; 
    return value; 
} 

- (CGLayerRef)layerValue 
{ 
    CGLayerRef layer; 
    [self getValue:&layer]; 
    return layer; 
} 
관련 문제