Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 29, 2012, 05: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 02:32 PM. Reason: Problem solved
Boegs is offline   0 Reply With Quote
Old Jan 30, 2012, 03:28 AM   #2
gnasher729
In Time-Out
 
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, 02: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

Similar Threads
thread Thread Starter Forum Replies Last Post
how to convert bytes array to image MacccUser iPhone/iPad Programming 7 Mar 21, 2014 07:12 AM
Possible to access Origional image file from texted image? (to view metadata) inck243 iPhone Tips, Help and Troubleshooting 2 Dec 8, 2013 12:47 AM
Can I install Photoshop Elements & Premiere Elements 11 on Macbook Air 2012? janjan77 MacBook Air 3 Mar 30, 2013 02:44 PM
rMBP13 - Aperture or Elements/Priemer Elements RickG MacBook Pro 9 Nov 2, 2012 10:33 AM
Multiplying array elements by 3. C program on Xcode Mugambo Mac Programming 3 Jul 26, 2012 03:02 AM

Forum Jump

All times are GMT -5. The time now is 06:17 PM.

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

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