【问题标题】:how to aggregate information on UIImage?如何聚合 UIImage 上的信息?
【发布时间】:2013-11-10 13:25:18
【问题描述】:

我想在我的 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];
}

问题是我认为每次绘制整个图像仅增加 1 行非常昂贵,有人知道如何优化它吗?

【问题讨论】:

  • 信息太少,无法给您准确的答案...通常,drawRect 覆盖工作缓慢,这是“非常昂贵的”,是的。但是,在某些情况下,您可能会摆脱此类操作。如果您只需要在视图上显示一行 - 使用此行添加子视图。如果您需要一些更难的绘图 - 使用蒙版,alphas。如果您确实需要这是“带线条的图像”(例如,用于保存到文件或上传到服务器) - 是的,您需要像您一样在图像上画线。附:尽量让你的问题更具体,提供更多细节。
  • 是屏幕显示还是不显示?是否应该在 drawRect 方法中完全添加该行?
  • 为了更清楚,我编辑了我的问题。

标签: ios quartz-2d


【解决方案1】:

好的,没有人回答,但这是我找到的解决方案:

  1. 首先创建位图上下文:

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

  3. 现在使用以下方法将上下文中的信息返回到 bitamp 图像中:

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

干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多