How to transform this NSImage code for use in iOS

Discussion in 'iOS Programming' started by hellomoto4, Jun 23, 2012.

  1. hellomoto4 macrumors 6502a

    hellomoto4

    Joined:
    Jul 11, 2008
    Location:
    Australia
    #1
    I am attempting at turning some code written for Mac OS X into an iOS application. This is the working code for OS X:
    Code:
    // To create the images on the view:
    NSRect imageRect = NSMakeRect(24+(i*80), ypos, 72, 96);
        NSImageView *theImageView=[[NSImageView alloc] initWithFrame:imageRect];
        [theImageView setBounds:imageRect];
        [self addSubview:theImageView];
        [theImageView setImage:[tCard image]];
        [self.imageviews addObject:theImageView];
    
    // To remove the images on the view:
        for (NSImageView *timageview in self.imageviews) {
        [timageview removeFromSuperview];
    }
    [self.imageviews removeAllObjects];
    
    At the moment, I have:
    Code:
    //To create the image on the view
    UIImageView * theImageView = [[UIImageView alloc] initWithFrame:CGRectMake(24+(i*80), ypos, 72, 96)];
        [self addSubview:theImageView];
        [theImageView setImage:[tCard image]];
        [self.imageviews addObject:theImageView];
    
    // To remove the images on the view:
     for (UIImageView *timageview in self.imageviews) {
        [timageview removeFromSuperview];
    }
    [self.imageviews removeAllObjects];
    
    However! The removing of images does not work. The images are shown fine on the view, but I cannot remove them. To be totally honest, I'm not 100% sure about how the sample code completely works, but I am hacking away and trying to work it out, hence why I seem to be sort of half way there, but can't get the images to remove :'(

    I appreciate your following input! Any questions or more information required let me know.


    Note: This is how self.imageviews is initialized:
    Code:
    - (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.imageviews = [[NSMutableArray alloc] initWithCapacity:4]; 
    }
    return self;
    }
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    Nothing jumps out at me so if you haven't done it, place NSLog statements before the removal loop and within it to verify that it is actually executing. Or use break points to the same affect.

    At this point I suspect you are not executing that removal code or that the imageviews array object is nil.
     
  3. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    Have you tried using a more common C for or while loop?

    Personally, I rarely use loops other than those...
     
  4. hellomoto4 thread starter macrumors 6502a

    hellomoto4

    Joined:
    Jul 11, 2008
    Location:
    Australia
    #4
    Sorry, should have mentioned that I've used breakpoints and nothing. It would seem that the problem is in the for loop, but I just can't put my finger on it.


    I've tried to incorporate different loops but to no avail. Perhaps my code was sketchy though. What would you recommend to use?
     
  5. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #5
    Either use breakpoints or NSLogs in your loop. Put one before entering the loop, one inside the loop before anything happens, and another in it after the removal occurs, and then one more entirely outside the loop.

    That should let you know exactly what is causing the crash.

    One possibility... When remove is called, the index of each other image will be changed. Thus if your loop is calling different indexes each time, it could mess up. Imagine, for example, you have three images A, B, and C.

    Loop is entered, the image at index 0 is removed. Thus A is gone, B is now at index 0, and C is at index 1. The image at index 1 is removed, thus C is gone and B is all that's left, at index 0. The image at index 2 is removed, but, hey wait a minute, there's nothing at index 2, so crash.

    There are two possible ways of solving this that come to mind:

    1 - Get the count and start from the end and work your way back through the array, thus indexes won't change on you.
    2 - Use a while loop. Just check each time that there are any subviews (count will work) and remove any image (at index 0 should always work, if it has been confirmed there is a subview.)

    I haven't actually checked to see if this works (I'm on an iPhone, not a computer with Xcode), but I feel like I have a memory of having to have done this over a year ago on Egg Drop!...
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Please explain. Do you mean the breakpoint wasn't being hit? If that's what happened, then the code isn't being executed. You need to look earlier in your code. And try posting complete code, in complete methods.

    In general, please do the following:
    1. Describe what you expected to happen.
    2. Describe what actually happened.
    3. Post complete code, not isolated fragments.

    Example: "I set breakpoints and expected the program to stop when one was hit. The program didn't stop at any breakpoint. It didn't stop at all."


    Post your code. If by "sketchy" you mean "possibly wrong", how could we determine whether it's right or wrong except by seeing the actual code?
     

Share This Page