How to change the image in UIImageView???

Discussion in 'iOS Programming' started by tomgoodenough, Nov 2, 2010.

  1. tomgoodenough macrumors newbie

    Joined:
    Sep 10, 2010
    #1
    I have a real problem here, i really cant figure it out.

    I'm new to xcode so please explain stuff simply. ok, i have an ibaction that says this:


    Code:
    
    -(IBAction)ChangeImage1 {
    	
    	imageView.image = [[UIImage alloc] initWithContentsOfFile: @"Image1.jpg"];
    	
    }
    
    
    This changes the image fine and i have 6 images, but the memory leaks REALLY bad and my app just crashes after showing 1 image. How do i do this without massive leaks? any ideas? please explain simply!!!
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You are using alloc/init to create the image. This means that you are the owner of that and have to ensure it is released. This is covered in the Memory Management Rules.

    The simplest answer is to autorelease the object:

    Code:
    -(IBAction)ChangeImage1
    {
    	imageView.image = [[[UIImage alloc] initWithContentsOfFile: @"Image1.jpg"] autorelease];
    }
    
    Or use a method that will return an autorelease UIImage.

    Either way you need to read the memory management document I linked to as this is basic stuff you will have to understand.
     
  3. tomgoodenough thread starter macrumors newbie

    Joined:
    Sep 10, 2010
    #3
    My app still isn't working, with that code the pictures dont even show up. I tried this:

    Code:
    imageView.image = [[UIImage imageNamed:@"Image1.jpg"] autorelease];
    
    but it still didnt work and just crashed like before. ????????????????

    Maybe should i get them from online instead of from the resources? but then ipod touch users wont see them unless on wi fi.....
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    That code is wrong. It autoreleases an image that you don't own.

    If that doesn't make sense, then you need to re-read the Memory Management Guide that was already pointed out to you. If it still doesn't make sense, you need to go back to some other fundamentals.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Stop. Step back. Take a breath. Chill. Resist the temptation to throw stuff against the wall to see what sticks. Spend time to understand the real problem and then spend time to understand whatever solutions you attempt.

    And you were certain they were showing up before? Perhaps the images you think you're referencing aren't available. I would suggest some basic debugging of breaking down the issue into smaller pieces you can test against and verify conclusions.

    Memory leaks usually don't result in crashing that quickly. So, there is probably some other cause to the crash. That is what you should investigate. Set up breakpoints and/or look at console messages and/or look at crash logs. Make sure you understand why it is crashing before attempting to fix it.

    No, if they are static images, getting them from resources will be much more efficient and speedy then getting them online.
     
  6. tomgoodenough thread starter macrumors newbie

    Joined:
    Sep 10, 2010
    #6
    sorry yeah i am getting annoyed, its just im only 12 and i dont know xcode that well. Maybe it would just be easier to make actions for each image, and make them setHidden:NO when the button is tapped, would that be easier than messing around with imageviews?
     
  7. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #7
    Robbie originally advised you to use an autorelease with your alloc/init pair OR use a factory method that returns an autoreleased object (any class method that returns an object and doesn't begin with the word 'new'). Not both, which is why you are having a problem.

    You can do this:

    Code:
    imageView.image = [[[UIImage alloc] initWithContentsOfFile:@"file.jpg"] autorelease];
    
    OR

    Code:
    imageView.image = [UIImage imageNamed:@"file.jpg"];
    
    I would probably use the latter as a matter of style.

    The imageNamed method returns an autoreleased object; calling autorelease on it again will result in over-releasing and a crash.

    I echo everybody else's suggestion that you go and re-read the memory management rules document.
     
  8. tomgoodenough thread starter macrumors newbie

    Joined:
    Sep 10, 2010
    #8
    hmmm still not working am i supposed to be declaring the images as properties?
     
  9. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #9
    Have you added the images to your project or are you just trying to reference them on the file system?
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    And throwing random solutions to a poorly-understood problem is probably not going to decrease your annoyance.

    Really? Then you're not even supposed to be posting on these forums and have lied in order to register. The minimum age is 13. I'm consulting the other mods to see how we normally handle these kinds of situations.

    Probably more likely that not knowing Objective-C well enough is the real issue.

    Instead of just proposing a new solution, you should try to understand why the current approach is not working before you abandon it.

    You really need to start applying some basic debugging skills and report on the outcome of those rather than just continue with the "not working" reports. What, specifically, is not working? Have you verified your assumptions made in the code? Are you getting compile-time errors and/or warnings? How about run-time? If it's still crashing, what do the crash logs say? Etc. Answers to these things are much more helpful than "still not working".
     

Share This Page