本文介绍了使用 CGContextRef 在大尺寸图像上绘制线条后,在 iPad 中接收内存警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am drawing lines on UIImage by using CGContextRef.My images are very large in resolution like 3000x4500.It doesn't give me memory warning if I draw a single line but if I would draw more than one line then it gives memory warning and after that my app crashes.Tried to release the CGContextRef object but got an error. My code :

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;

推荐答案

仅图像就需要 54MB 内存(3000 * 4500 * 4).

Just the image will require 54MB of memory (3000 * 4500 * 4).

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

Since only a portion can be displayed at a time consider dividing the image into several sections like map tiles as used in Apple Maps.

这篇关于使用 CGContextRef 在大尺寸图像上绘制线条后,在 iPad 中接收内存警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 15:15