Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何更改CGContextRef的大小_Iphone_Objective C_Ios_Core Graphics - Fatal编程技术网

Iphone 如何更改CGContextRef的大小

Iphone 如何更改CGContextRef的大小,iphone,objective-c,ios,core-graphics,Iphone,Objective C,Ios,Core Graphics,如何在创建CGContextRef后增加其大小 if(UIGraphicsBeginImageContextWithOptions != NULL) { UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0); } else { UIGraphicsBeginImageContext(CGSizeMake(width, height)); } CGContextRef ctr = UIGra

如何在创建CGContextRef后增加其大小

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

我可以更改ctr的大小吗?

尝试在没有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);
}

我和你有同样的问题。我开发了一个函数来调整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
手动创建上下文,它将起作用


希望在您询问后能有所帮助…

这不是我需要的,我需要增加绘图空间。请查看链接。它帮助您使用makeContextOfSize创建CGContext