0

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;
iOS_Learner
  • 159
  • 9
  • @Zaph If you could elaborate .... – iOS_Learner Jan 29 '15 at 05:08
  • Sorry, bad comment, I guess you can't due to all the different colors, widths. – zaph Jan 29 '15 at 05:17
  • I think you should look at this example ZoomingPDFViewer: https://developer.apple.com/library/ios/samplecode/ZoomingPDFViewer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010281 You should look at `TiledPDFView` – Cy-4AH Jan 29 '15 at 09:22
  • @Zaph http://stackoverflow.com/questions/28272062/app-crashes-due-to-memory-warning-on-ipad-3/28272852#28272852 – iOS_Learner Feb 02 '15 at 07:36

1 Answers1

1

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

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.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • I have found something interesting.If image is zoomed in, then it doesn't give memory warning ... no matter how many lines I have drawn on the image.... Although image size is same .. – iOS_Learner Jan 29 '15 at 11:37
  • My guess is that when zoomed in the drawing is only being clipped to the visible portion. – zaph Jan 29 '15 at 13:32
  • I have also tried to use the CGContextFlush() .. but didn't get the result.Any solution other than tiles. :( – iOS_Learner Jan 29 '15 at 16:03
  • tried different things but couldn't get the success :( – iOS_Learner Jan 30 '15 at 11:44