0

I am creating a custom transition between two view controllers. To do this I am grabbing screenshots of custom areas on the top and bottom of the view in order to animate them out.

However, whenever I do this an ImageIO_JPEG_Data is created (about 7 - 8 mb for each transition) that is never eliminated or released. (You can see three in the instruments screen shot below).

enter image description here

Trolling stackoverflow answers to similar questions on I have tried distinctly running this on the main thread with dispatch_async(dispatch_get_main_queue(), ^{ and adding an @autorelease pool but to not avail.

Am I missing something or is there a better way to create these screenshots that would not create a memory leak?

//Get Top View
CGSize photoSize = CGSizeMake(self.view.frame.size.width, topHeight);

UIGraphicsBeginImageContextWithOptions(photoSize, NO, 0.0f);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.layer.contents = nil;

topTransitionView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, snapshot.size.width, snapshot.size.height)];
topTransitionView.image = snapshot;

//Get Bottom View
CGRect photoRect = CGRectMake(0, imageFrame.origin.y + imageFrame.size.height, self.view.frame.size.width,self.view.frame.size.height - (imageFrame.origin.y + imageFrame.size.height));

if (photoRect.size.height <= 0) {
    photoRect.size.height =1;
}

UIGraphicsBeginImageContextWithOptions(photoRect.size, NO, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -photoRect.origin.x, -photoRect.origin.y);
[self.view.layer renderInContext:ctx];
UIImage *bottomSnapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.layer.contents = nil;

bottomTransitionView = [[UIImageView alloc]initWithFrame:CGRectMake(0, imageFrame.origin.y + imageFrame.size.height, bottomSnapshot.size.width, bottomSnapshot.size.height)];
bottomTransitionView.image = bottomSnapshot;
Chase S
  • 448
  • 3
  • 20
  • You're showing us the instantiation of `topTransitionView` and `bottomTransitionView`, but you're not showing us where that's added (or more importantly, where the prior one is removed). Are you doing a `removeFromSuperview` somewhere before instantiating a new set of these views? Have you run this through the static analyzer ("Analyze" on the "Product" menu)? – Rob Feb 15 '15 at 06:32
  • Yes, I call `removeFromSuperView` on all the `topTransitionView` and `bottomTransitionView` as well as setting them to nil after the transition completes. – Chase S Feb 15 '15 at 06:35
  • You can try `drawViewHierarchyInRect` (iOS 7+), but I doubt the problem rests in `renderInContext`. By the way, with the allocations tool, it can show you where the leaked objects where allocated. I'd also check `po [[UIWindow keyWindow] recursiveDescription]` at the `(lldb)` prompt (or use the new Xcode 6 view debugger) to make sure there isn't stuff lingering about. – Rob Feb 15 '15 at 06:43
  • Yeah. Or something abandoned (e.g. I don't use `self.view.layer.contents = nil` construct, but rather remove the `UIKit` or `CALayer` stuff explicitly, though I doubt that's the issue). Between Allocations tool, static analyzer, and view debugging tool, I was hoping you'd be able to identify the category of problem, making further diagnosis easier. (Sorry, this sort of thing is tough to do remotely.) But it feels like an image is not getting released somewhere, and I don't think it's the snapshotting stuff, personally. – Rob Feb 15 '15 at 06:51
  • Thanks for the advice although that last part "at the (11db) prompt" I am not sure what you are referring to. – Chase S Feb 15 '15 at 06:54
  • Run the app in the debugger, pause execution (not stop, but just pause), at which point a `(lldb)` prompt will show up on the console in Xcode. You can then enter `po [[UIWindow keyWindow] recursiveDescription]` and it will show you a nice text representation of the view hierarchy. It's useful for making sure you don't have stuff lingering about in the view hierarchy. See [this answer](http://stackoverflow.com/a/18492352/1271826) for examples of how to view constraints and view hierarchy from the Xcode debugger. – Rob Feb 15 '15 at 07:02

0 Answers0