PDA

View Full Version : [Resolved] Storing an image of UI elements in an array




Boegs
Jan 29, 2012, 04:20 PM
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:
@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:

// 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:



gnasher729
Jan 30, 2012, 02:28 AM
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:
@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:

// 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
Jan 30, 2012, 01:31 PM
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!