Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 29, 2012, 04:20 PM   #1
Boegs
macrumors newbie
 
Join Date: Oct 2010
Location: London, UK
Storing an image of UI elements in an array

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

Last edited by Boegs; Jan 30, 2012 at 01:32 PM. Reason: Problem solved
Boegs is offline   0 Reply With Quote
Old Jan 30, 2012, 02:28 AM   #2
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Boegs View Post
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
I am missing the point in your code where subViewImages is being created.
gnasher729 is offline   0 Reply With Quote
Old Jan 30, 2012, 01:31 PM   #3
Boegs
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Location: London, UK
Quote:
Originally Posted by gnasher729 View Post
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!
Boegs is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Best Way to Store a Paragraph in an Array? jpmcgrath iPhone/iPad Programming 2 Jul 20, 2011 04:38 PM
Storing line points in an array :( hodgey87 iPhone/iPad Programming 5 Oct 21, 2010 12:21 PM
What are the UI elements in the itunes and marvel app?? rthauby iPhone/iPad Programming 16 Jun 1, 2010 12:51 PM
Assigning the value in an array to variable in C subsonix Mac Programming 7 Dec 16, 2008 05:06 AM
java: why does this segment of code reverse the order of the elements of an array? macman2790 Mac Programming 7 Feb 15, 2007 09:57 AM


All times are GMT -5. The time now is 02:28 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC