Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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
Ios 如何在持久画布上绘制并在视图中绘制?_Ios_Image_Drawing_Cgcontext_Persistent - Fatal编程技术网

Ios 如何在持久画布上绘制并在视图中绘制?

Ios 如何在持久画布上绘制并在视图中绘制?,ios,image,drawing,cgcontext,persistent,Ios,Image,Drawing,Cgcontext,Persistent,我正在为iOS编写一个小的绘画应用程序。我正在对UIView ad进行子类化,在其drawRect:方法中执行计算。这一切都很好,直到我开始有很多对象(实际上是多段线),然后性能开始下降 - (void)drawRect:(CGRect)rect { [super drawRect:rect]; CGContextRef imageContext = UIGraphicsGetCurrentContext(); int i, j; for(i = 0; i &

我正在为iOS编写一个小的绘画应用程序。我正在对UIView ad进行子类化,在其drawRect:方法中执行计算。这一切都很好,直到我开始有很多对象(实际上是多段线),然后性能开始下降

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    CGContextRef imageContext = UIGraphicsGetCurrentContext();

    int i, j;
    for(i = 0; i < [_strokes count]; i++) {
        NSMutableArray *stroke = [_strokes objectAtIndex:i];
        if([stroke count] < 2) {
            continue;
        }
        CGContextSetStrokeColorWithColor(imageContext, [[_penColours objectAtIndex:i] CGColor]);
        for(j = 0; j < [stroke count]-1; j++) {
            CGPoint line[] = {
                [[stroke objectAtIndex:j] CGPointValue],
                [[stroke objectAtIndex:j+1] CGPointValue]
            };

            CGContextSetLineWidth(imageContext, _brushSize);
            CGContextSetLineCap(imageContext, kCGLineCapRound);
            CGContextSetLineJoin(imageContext, kCGLineJoinRound);

            CGContextAddLines(imageContext, line, 2);
            CGContextStrokePath(imageContext);
        }
    }

    CGContextFlush(imageContext);
}

看起来至少有一部分问题是你保留了你的图片,但你从来没有发布过它。每次为_image指定一个新值时,都会泄漏上一个映像,这会很快填满内存


除此之外,您可能应该在屏幕外的绘图中使用位图,而不是位图。您可以随时绘制图层,然后用-drawRect:method将其复制到屏幕上。

我会尽快尝试,并让您知道,谢谢您的提示。您好,我刚刚花了四个小时试图了解如何在CGLayers上绘制,但我没能完成多少工作。有什么建议吗?
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    UIGraphicsBeginImageContext(CGSizeMake(1024, 768));
    CGContextRef imageContext = UIGraphicsGetCurrentContext();

    if(_image != nil) {
        CGContextDrawImage(imageContext, CGRectMake(0, 0, 1024, 768), [_image CGImage]);
    }

    int i, j;
    for(i = 0; i < [_strokes count]; i++) {
        NSMutableArray *stroke = [_strokes objectAtIndex:i];
        if([stroke count] < 2) {
            continue;
        }
        CGContextSetStrokeColorWithColor(imageContext, [[_penColours objectAtIndex:i] CGColor]);
        for(j = [stroke count]-2; j < [stroke count]-1; j++) {
            CGPoint line[] = {
                [[stroke objectAtIndex:j] CGPointValue],
                [[stroke objectAtIndex:j+1] CGPointValue]
            };

            CGContextSetLineWidth(imageContext, _brushSize);
            CGContextSetLineCap(imageContext, kCGLineCapRound);
            CGContextSetLineJoin(imageContext, kCGLineJoinRound);

            CGContextAddLines(imageContext, line, 2);
            CGContextStrokePath(imageContext);
        }
    }

    _image = UIGraphicsGetImageFromCurrentImageContext();
    [_image retain];
    UIGraphicsEndImageContext();

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, CGRectMake(0, 0, 1024, 768), [_image CGImage]);
}