Ios 如何在UIImage上聚合信息?

Ios 如何在UIImage上聚合信息?,ios,quartz-2d,Ios,Quartz 2d,我想在我的UIIMage上每画1条线,现在我通过以下方式完成: -(void)drawRect { UIGraphicsBeginImageContext(myImage.size); code to draw line on current context... draw previous info from myImage: [myImage drawInRect:myRect]; //store info from context bac

我想在我的UIIMage上每画1条线,现在我通过以下方式完成:

-(void)drawRect
{


    UIGraphicsBeginImageContext(myImage.size);


    code to draw line on current context...

    draw previous info from myImage:
    [myImage drawInRect:myRect];

    //store info from context back to myImage
    myImage=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


    //append the image on the right side of current context:
    [myImage drawInRect:myRightRect];
}

问题是,我认为每次只为添加一条线绘制整个图像非常昂贵,有人知道如何优化吗?

好的,没有人回答,但我找到了以下解决方案:

  • 首先创建位图上下文:

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    
    CGContextRef bitmapContext = CGBitmapContextCreate (NULL, pixelsWide, pixelsHigh, 8,
                                                    0, colorSpace,(kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));
    CGColorSpaceRelease(colorSpace);
    
  • 在此上下文中绘制线条和形状

  • 现在使用以下命令将信息从上下文返回到bitamp图像:

    CGImageRef leftScaleImage = CGBitmapContextCreateImage(bitmapContext);
    UIImage* leftScaleUIImage = [UIImage imageWithCGImage:leftScaleImage];
    

  • 干杯

    信息太少,无法给你准确的答案。。。一般来说,drawRect重写工作缓慢,这是“非常昂贵的”,是的。但是,在某些情况下,您可能会放弃此类操作。如果只需要在视图上显示一行,请使用此行添加子视图。如果你需要一些更难的画-玩面具,阿尔法。如果您确实需要“带线条的图像”(例如,用于保存到文件或上载到服务器)-是的,您需要像您一样在图像上绘制线条。另外,请尽量使您的问题更具体,提供更多细节。它是否用于在屏幕上显示?是否应该在drawRect方法中精确添加行?为了更清楚,我编辑了我的问题。