0

I am drawing lines on UIImage and my image is in UIImageView. First time drawing process goes fine and I assign the new Image to UIImageView but when I repeat the process it gives me memory warning and app crashes:

Terminating in response to backboard's termination.

I have profiled my app and the CG raster data was taking 273 MB and overall its 341 MB Live Bytes. Also wrapped code in in autorelease pool but didn't get the success. 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;
KlimczakM
  • 12,576
  • 11
  • 64
  • 83
iOS_Learner
  • 159
  • 9
  • while animating are you loading any images ? – Vineesh TP Feb 02 '15 at 07:03
  • @VineeshTP yes I am loading high resolution images ... and my loop draws all the lines on the image. Image resolution is 7000X4000. when loop ends then I assign the image to UIImageView. – iOS_Learner Feb 02 '15 at 07:06
  • @VineeshTP but in my case I can't compromise the Image size.. what else could be the solution ? – iOS_Learner Feb 02 '15 at 07:34
  • I see nothing wrong with the code you posted. Could you pleas try to check if the image is the one responsible for this by commenting out the last line you posted (assigning the image to the image view). You will not see the result but the execution will be all the same so check if it still leaks the memory. – Matic Oblak Feb 02 '15 at 07:40
  • @MaticOblak ok let me try – iOS_Learner Feb 02 '15 at 07:45
  • @MaticOblak I have followed your instruction... yes my app didn't crash and off course UIImageView was empty because i didn't assign image to UIImageView. – iOS_Learner Feb 02 '15 at 07:52
  • Well it is the good sign we pinpointed the issue but a bad sign to where it is. It seems if you assign the image to the image view it is retained for a at leas a bit longer. So explain how frequently these calls are made, are they only once per touch down or can this method be called multiple times? – Matic Oblak Feb 02 '15 at 07:54
  • @MaticOblak I have also tried another approach that is , I reduced the size of my image forcefully and after that drew lines on it and assigned the image to UIImageView , it went fine... so It seems that size is an issue in my case . – iOS_Learner Feb 02 '15 at 07:54
  • @MaticOblak but the problem is I can not compromise the image size.. I have to keep the image in its original size. – iOS_Learner Feb 02 '15 at 07:55
  • Could be or you just decreased it so much you need more call to see the difference. – Matic Oblak Feb 02 '15 at 07:55
  • Well you must understand what you are doing is not really a standard approach. You could draw those lines on the view and then on the image only once for exporting if needed at all. This way you could also to the the tilling on your image scroll view to greatly reduce the memory footprint. – Matic Oblak Feb 02 '15 at 07:58
  • @MaticOblak it is related to users touch ... if he touches the image then this method will be called and lines will be drawn. – iOS_Learner Feb 02 '15 at 07:58
  • @MaticOblak ok ... Well in fact its not compulsory to make the lines exactly on the image .. because I have to draw lines again and again .... means lines will not drawn permanently on the image. – iOS_Learner Feb 02 '15 at 08:02
  • @MaticOblak So would it be possible to draw lines on the view which will be on the image , and that view will have empty background. am I right ? – iOS_Learner Feb 02 '15 at 08:03
  • 1
    You are correct. You may more or less copy your code and put it into another view's drawRect method (you override it). What you need to add is to draw a fullscreen rect with a fill color set to clear color. Also remove all the image relevant code. Then this view needs to be added to you image view (or the scroll view). Also after every change in lines you need to call setNeedsDisplay on the view drawing the lines. – Matic Oblak Feb 02 '15 at 08:09
  • Since you probably do not want to port all the code to this subview I suggest you rather create a delegate with method like drawLines which will be called in the drawRect method. This is really optional but will produce a nicer code since you will still control the gesture events where you do now. – Matic Oblak Feb 02 '15 at 08:11
  • Down vote answer please mention the reason – Vineesh TP Feb 02 '15 at 08:12
  • @Farrukh: Max resolution used in iOS device is 1024x1024. we can't user mare than that size. use 2x and 3x images for respective device sizes. – Vineesh TP Feb 02 '15 at 08:13
  • @MaticOblak means I will be drawing lines on the view again and again. Ok . I am going to do this, lets see the results. – iOS_Learner Feb 02 '15 at 08:14
  • @VineeshTP Thanks for your answer ... I didn't do the down vote..... and please read my comment that , I can't compromise the image size. – iOS_Learner Feb 02 '15 at 08:16
  • @MaticOblak Ok I will follow as you suggested..then will update you the results. – iOS_Learner Feb 02 '15 at 08:17
  • @MaticOblak I have created on UIView and placed on top of the UIImageView.Background colour is set to be clear color. Now first I am adding this UIView as a subview of UIImageView and then I am calling setNeedsDisplay so it calls the drawrect method of that view which I have overridden but its not drawing lines that view. – iOS_Learner Feb 03 '15 at 06:59
  • @MaticOblak I have set the frame size of my view equal to the image size . – iOS_Learner Feb 03 '15 at 07:04
  • Try seeing some example of drawing lines to an UIView and see what could be wrong pleas. – Matic Oblak Feb 03 '15 at 07:44
  • @MaticOblak I have tried different solutions but no success. and the problem is only one line of code... means When I assign the image which was returned by the UIGraphicsGetImageFromCurrentImageContext() , to UIImageView then my app crashes.I fail to understand how to handle this issue. – iOS_Learner Feb 06 '15 at 05:26
  • @MaticOblak http://stackoverflow.com/questions/28359164/uigraphicsgetimagefromcurrentimagecontext-crashes-the-app-on-ipad-3?noredirect=1#comment45064921_28359164 – iOS_Learner Feb 06 '15 at 08:56
  • @MaticOblak http://stackoverflow.com/questions/28601177/restrict-the-overlay-view-frame-in-uisearchdispalycontroller – iOS_Learner Feb 19 '15 at 11:02

2 Answers2

0

So it just happened I stumbled onto a similar issue. I was assigning an image to the image view where the image itself was previously retained by other objects, being processed and such... The result was that the image view did indeed leak everyone of those images somehow.

The solution I used was to create a copy of the image on the level of the CGImage before assigning it to the image view. I guess there must be an issue with the bitmaps or something. Anyway try creating a copy like this:

CGImageRef cgCopy = CGImageCreateCopy(originalImage.CGImage);
UIImage *copiedImage = [[UIImage alloc] initWithCGImage:cgCopy scale:originalImage.scale orientation:originalImage.imageOrientation];
    CGImageRelease(cgCopy);
imageView.image = copiedImage;
Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
  • I have tried your solution : my app still crashed , only one thing has changed , it crashes after performing big number of iteration. In normal case it works fine. Thanks a lot for your assistance – iOS_Learner Feb 23 '15 at 06:06
  • So this might have fixed the image view issue? There might be another leak elsewhere. You could try commenting out the image assignment and the image copy part and see if you get the crash with your "big number of iterations" anyway. – Matic Oblak Feb 23 '15 at 07:56
-2

Max resolution used in iOS device is 1024x1024. we can't use more than that size. use 2x and 3x images for respective device sizes.

Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
  • 3
    Not that I down voted but this answer is wrong in so many ways. The 2x and 3x are the suffixes used to grab the appropriate image resource from the resource folder, nothing more. Also the content scales are 1x, 2x, 3x which are in place only to preserve the view coordinate system when using frames, bounds. This has nothing to do with the image size at all. And about the 1024x1024 do you have any reference to where you got that because it is very very far from true. – Matic Oblak Feb 02 '15 at 08:29