1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Resolved Storing an image of UI elements in an array

Discussion in 'Mac Programming' started by Boegs, Jan 29, 2012.

  1. Boegs, Jan 29, 2012
    Last edited: Jan 30, 2012

    macrumors newbie

    #1
    I am trying to enumerate through all subviews of a view and create a snapshot image of each NSControl item and add it to a NSMutableArray. The ultimate goal is to allow a user to rearrange the various UI elements.

    I have subclassed NSView and added two properties:
    Code:
    @interface MyContent : NSView
    
    
    @property (strong) NSMutableArray *subViewObjects;
    @property (strong) NSMutableArray *subViewImages;
    
    @end
    In IB, I added two TextViews to the main view and set the class of the view to MyContent.

    Then, in the MyContent implementation, I have the following, in awakeFromNib:

    Code:
       // Copy all the items on the view
        subViewObjects = [[self subviews] mutableCopy];
        NSLog(@"%@",subViewObjects);
        
        // Create images of all the views
        NSUInteger i, count;
        for (i = 0, count = [subViewObjects count]; i < count; i++) {
            NSView *view = [subViewObjects objectAtIndex:i];
            NSImage *image = [[NSImage alloc] initWithSize:[view bounds].size];
            [image lockFocus];
            [view drawRect:[view bounds]];
            [image unlockFocus];
            [subViewImages addObject:image];
            NSLog(@"Number of subViewImages; %lu",[subViewImages count]);
        }
    Using various NSLog statements, it seems that the subViewObjects are correctly added to an array, but the subViewImages array does not get populated with images. I am convinced I have overlooked something really simple and it is driving me crazy! Does anybody know what I am doing wrong?

    I am using Xcode 4.2.1, with ARC and Lion (10.7.2)

    If anybody can point me in the right direction, I would be very grateful :confused::confused:
     
  2. macrumors G5

    gnasher729

    #2
    I am missing the point in your code where subViewImages is being created.
     
  3. macrumors newbie

    #3
    I hang my head in shame: I cannot believe I forgot to alloc/init the subViewImages array. Needless to say, once it is set up correctly, it works. Many thanks Gnasher!
     

Share This Page