Memory issue with scrollable plan

Discussion in 'iOS Programming' started by boyplunder, Oct 20, 2011.

  1. boyplunder macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #1
    I have an app project that I have been developing that, within the app, shows a view of a plan of a building in a modalViewController. The user can scroll around and zoom in or out to a controlled degree. I seem to have a memory issue, even though xcode doesn't report anything. The plan graphic is about 1.2mb, and the size of the graphic may be part of the issue. If so, is there any advice on using images in this way.

    I've got to the point where I just stare at it and can't see the wood for the trees. For instance, do I have to release tempImageView?

    I am working in Xcode 3.2.6 at the moment and I get no errors or issues reported.

    Code:
    - (void)viewDidLoad {
        
    	UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Buildingplan.jpg"]];
    	
    	self.imageView = tempImageView;
    	
    	scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
    	[scrollView setContentOffset:CGPointMake(685.0f, 315.0f)];
    	scrollView.maximumZoomScale = 2.1;
    	scrollView.minimumZoomScale = .6;
    	scrollView.clipsToBounds = YES;
    	[scrollView setDelegate:(id<UIScrollViewDelegate>)self];
    	[scrollView addSubview:imageView];
    	
    	scrollView.contentSize = CGSizeMake(imageView.image.size.width*0.50, 
    										imageView.image.size.height*0.50);
    	
    	imageView.frame = CGRectMake(imageView.frame.origin.x, 
    								 imageView.frame.origin.y, 
    								 imageView.frame.size.width*0.50, 
    								 imageView.frame.size.height*0.50);
    	
    	[imageView release];
    	[scrollView release];
    	
    	[super viewDidLoad];
    }
    If someone could let me have an appraisal of this code, I would appreciate the help.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
  3. boyplunder thread starter macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #3
    I have read explanations on management a few times, but maybe not fully understood.

    If I understand this correctly, I created and own UIImageView using the alloc method, so I need to release that myself. I didn't create imageview or scrollview, so they are released automatically.

    Surely I don't release it within ViewDidLoad, as I am using it after it is loaded, but I must release it in dealloc at the end or when the view is dismissed.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    First, some terminology: UIImageView is a class name. You don't get to create or own it.

    You did however create an instance of it, which you've called tempImageView. You do own that. So you'll need to release that before the method is finished.

    What is "it"?
     
  5. boyplunder thread starter macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #5
    By 'it' I mean tempImageView.
    I tend to think of 'release' as 'empty', which isn't good if I want someone to use the graphic while the view is loaded. Releasing when the view is dismissed makes more sense to me, but I know it doesn't work that way.

    I have some years in VB, which is probably where I get that from.
     
  6. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #6
    If the size of the graphic is an issue, you can try looking at CATiledLayer to draw your content. Basically, the tiled layer will only hold the part of the image that is currently on the screen. If part of the image is not visible, you do not have to hold it in memory. This can help if you have large bitmap images that are giving you memory problems. There should be some sample code from Apple that walks through how to use this technique.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Think of release not as 'empty' but rather 'i no longer claim ownership' instead.

    So, back to the original concern:

    Because you have assigned tempImageView to self.imageView (assuming imageView is a property with a retain attribute), it will still be retained for use elsewhere. Therefore, since tempImageView is going away after viewDidLoad: is done (because it's a local variable), you should release any ownership claims it has too.

    And since you are retaining imageView in the viewDidLoad:, it would make sense to release it in the viewDidUnload:. Make sense?
     
  8. boyplunder thread starter macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #8
    Thanks Dejo.
    I have spent today going over this a couple of times for myself and think I have cleaned up the initial problem. [As well as my own misunderstandings.]

    North Bronson has pointed me in the direction of CATiledLayer, which seems to answer the problem of large graphics and viewing with not much memory. I'm going to read up on this and see how it works. Having read some already, it seems to be the right way to go.

    I'm currently working through the ScrollViewSuite sample code from Apple, so let's see where I get over the weekend.
     

Share This Page