Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Boegs

macrumors newbie
Original poster
Oct 27, 2010
2
0
London, UK
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:
 
Last edited:

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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:

I am missing the point in your code where subViewImages is being created.
 

Boegs

macrumors newbie
Original poster
Oct 27, 2010
2
0
London, UK
I am missing the point in your code where subViewImages is being created.

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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.