2012-10-12 6 views
3

생성 후 CGContextRef의 크기를 늘리려면 어떻게해야합니까?CGContextRef의 크기를 변경하는 방법

if(UIGraphicsBeginImageContextWithOptions != NULL) { 
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0); 
} 
else { 
    UIGraphicsBeginImageContext(CGSizeMake(width, height)); 
} 
CGContextRef ctr = UIGraphicsGetCurrentContext(); 

ctr의 크기를 변경할 수 있습니까?

답변

1

if-else 조건없이 시도하십시오. 마지막 코드 행만을 사용하여 CGContext를 생성하십시오.

는 여기에 내가 당신과 같은 문제가 있었다 간단한 예를

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
    CGContextSetLineWidth(context, 2.0); 
    CGContextMoveToPoint(context, 100,100); 
    CGContextAddLineToPoint(context, 200, 200); 
    CGContextStrokePath(context); 
} 
+0

이것은 내가 필요로하는 것이 아니며 그리기 공간을 늘려야한다. – Moonkid

+0

[this] (http://juliuspaintings.co.uk/cgi-bin/paint_css/more_cocoa_examples/090-CGImage-scale.pl) 링크 체크 아웃. makeContextOfSize를 사용하여 CGContext를 만드는 데 도움이됩니다. – Anil

0

을합니다. 나는 CGContext의 크기를 조정하는 기능 개발 :

void MPResizeContextWithNewSize(CGContextRef *c, CGSize s) { 
    size_t bitsPerComponents = CGBitmapContextGetBitsPerComponent(*c); 
    size_t numberOfComponents = CGBitmapContextGetBitsPerPixel(*c)/bitsPerComponents; 
    CGContextRef newContext = CGBitmapContextCreate(NULL, s.width, s.height, bitsPerComponents, sizeof(UInt8)*s.width*numberOfComponents, 
                CGBitmapContextGetColorSpace(*c), CGBitmapContextGetBitmapInfo(*c)); 
    // Copying context content 
    CGImageRef im = CGBitmapContextCreateImage(*c); 
    CGContextDrawImage(newContext, CGRectMake(0, 0, CGBitmapContextGetWidth(*c), CGBitmapContextGetHeight(*c)), im); 
    CGImageRelease(im); 

    CGContextRelease(*c); 
    *c = newContext; 
} 

UIGraphicsBeginImageContext(...) 기능에 의해 제공되는 컨텍스트 작동하는지 모르겠어요,하지만 당신은 CGBitmapContextCreate에 수동으로 컨텍스트를 작성하는 경우, 그것은 작동합니다.

당신이 물어봤을 때 도움이 되었으면 좋겠다.

관련 문제