0

I am building an app with Cocos3D in need of relatively a lot of screenshot in action:

imgRef = tmpDrawingVisitor.renderSurface.createCGImage;

    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docDir = [paths objectAtIndex:0];
    imgPath = [docDir stringByAppendingPathComponent: [[NSProcessInfo processInfo] globallyUniqueString]];

    _flippingpath = [imgPath stringByAppendingPathExtension:@"png"];
    //UIImage* uiImg= [UIImage imageWithCGImage: imgRef];
    uiImgprep= [UIImage imageWithCGImage: imgRef];
    //UIImage* uiImg= [UIImage imageWithCGImage: imgRef scale:0.5 orientation: UIImageOrientationUp];
    uiImg = [self imageWithImage:uiImgprep scaledToSize:CGSizeMake(512.0, 384.0)];
    imgData = UIImagePNGRepresentation(uiImg);
    [imgData writeToFile: _flippingpath atomically: YES];

What I do is taking a screenshot and save it down to Document folder. After that map it in a CCSprite to animate it like book flipping.

The code works fine, however the memory accumulatively adding up (each time about 12 Mb) and up to a point memory will overload.

How can I manage the memory in this case? I am developing in IOS 7.1.1, which it is already ARC.

sooon
  • 4,718
  • 8
  • 63
  • 116
  • Have you tried running it through Instruments (⌘ + I) and check for Leaks and Allocations ? – Starscream Jul 02 '14 at 09:12
  • I did and in Allocations the memory keep piling up. I must say I am not expert in Instrument and not familiar most of the descriptions at the bottom. – sooon Jul 02 '14 at 09:14
  • Seems like [this post](http://stackoverflow.com/questions/7800174/does-arc-work-with-core-graphics-objects) have the same issue. – sooon Jul 02 '14 at 10:21
  • [This post](http://stackoverflow.com/questions/8577894/setting-the-contents-of-a-calayer-to-a-cgimageref-on-ios-with-arc-on) suggest add in `(__bridge id)` to cast it into `id` so that ARC will work. But I have compile error when I add `uiImgprep= (__bridge id)[UIImage imageWithCGImage: imgRef];` which `imgRef` is `CGImageRef`. Any idea what is wrong? – sooon Jul 07 '14 at 16:37

1 Answers1

0

II don't know if I understand correctly what your app does, but if I do, have you considered keeping in memory the screenshot only for the current page, the previous and the next since those are the only ones you need.

You could have three images only in memory:

  • previous
  • current
  • next

When flipping forward you can free previous and then previous = current, and allocate next. When flipping backward you can free next, and then next = current, and allocate previous.

Since you're using ARC, to free an image, simply make sure you have no strong pointer references to it.

Am I understanding your problem ?

Starscream
  • 1,128
  • 1
  • 9
  • 22
  • Imagine it is a 3d scene and you walk around with dynamic camera following you. Then you can go to next scene, which the app will take a screenshot of the dynamic view, place at a Sprite then flip it like a book. So I guess your first suggestion don't quite fit. As for the second suggestion, How to make sure to not have strong pointer? Any reference link? – sooon Jul 02 '14 at 09:51