【问题标题】:Receiving Memory Warning in iPad after drawing lines on large size image with CGContextRef使用 CGContextRef 在大尺寸图像上绘制线条后在 iPad 中收到内存警告
【发布时间】:2015-01-29 04:51:43
【问题描述】:

我正在使用 CGContextRef 在 UIImage 上画线。我的图像的分辨率非常大,例如 3000x4500。如果我画一条线,它不会给我内存警告,但如果我要画多条线,那么它会给我内存警告,然后我的应用程序崩溃。尝试释放 CGContextRef 对象但出现错误。我的代码:

UIGraphicsBeginImageContext(imageView.image.size);

[imageView.image drawAtPoint:CGPointMake(0, 0)];


context2=UIGraphicsGetCurrentContext(); 
for(int i=0; i<kmtaObject.iTotalSize; i++)
    {
        kmtaGroup=&kmtaObject.KMTA_GROUP_OBJ[i];

        //NSLog(@"Group  # = %d",i);

        for (int j=0; j<kmtaGroup->TotalLines; j++)
        {

            lineObject=&kmtaGroup->Line_INFO_OBJ[j];

           // NSLog(@"Line # = %d",j);
           // NSLog(@"*****************");
            x0 = lineObject->x0;
            y0= lineObject->y0;
            x1= lineObject->x1;
            y1= lineObject->y1;
            color= lineObject->Color;
            lineWidth= lineObject->LinkWidth;
            lineColor=[self add_colorWithRGBAHexValue:color];
            linearColor=lineColor;

            // Brush width
            CGContextSetLineWidth(context2, lineWidth);
            // Line Color
            CGContextSetStrokeColorWithColor(context2,[linearColor CGColor]);


            CGContextMoveToPoint(context2, x0, y0);

            CGContextAddLineToPoint(context2, x1, y1);
            CGContextStrokePath(context2);

        }
    }


    newImage=UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    imageView.image=newImage;

【问题讨论】:

标签: ios objective-c uiimageview uiimage


【解决方案1】:

仅图像将需要 54MB 内存 (3000 * 4500 * 4)。

由于一次只能显示一部分,请考虑将图像分成几个部分,例如 Apple 地图中使用的地图图块。

【讨论】:

  • 我发现了一些有趣的东西。如果图像被放大,那么它不会给出内存警告......无论我在图像上画了多少线......虽然图像大小是一样的..
  • 我的猜测是,当放大绘图时,它只会被剪裁到可见部分。
  • 我也尝试过使用 CGContextFlush() .. 但没有得到结果。除了瓷砖之外的任何解决方案。 :(
  • 尝试了不同的方法,但没有成功:(
猜你喜欢
  • 2012-07-05
  • 2011-07-26
  • 2015-06-26
  • 1970-01-01
  • 2016-01-22
  • 2013-03-08
  • 1970-01-01
  • 2012-09-28
相关资源
最近更新 更多