NSUserDefaults And IKImageBrowser Object Arrays?

Discussion in 'Mac Programming' started by Darkroom, Oct 30, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i'm trying to figure out a way to automatically load images into the image browser from a specific datasource path on launch using Apple's ImageBrowser sample (download link).

    i'd like to add any image i drop into the image browser to it's own folder using the NSFileManager on the performDragOperation method, so that this folder would act as the image broswer's init data source on launch. but:

    A) im not sure how to tell the image broswer to load a folder on launch and display the images (but this is probably easy... maybe?).

    B) i'm concerned it will not maintain the saved order of the images that the user has placed these images in the image browser (this is where i would assume user defaults would come in?)

    any help would be great.
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    You can store the path of each image in an NSString, all the NSStrings in an array, store that array into the user defaults with setObject: forKey: and read it using stringArrayForKey:

    Now all you have to do is display it :D
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    how do i copy contents from the pasteboard to another folder? i'm thinking if i can just copy the pasteboard contents to a specific folder, i would be able to simply use that folder's contents as the image browser's initial datasource when the app launches

    Code:
    - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
    {
        NSData*			data = nil;
        NSPasteboard*	pasteboard = [sender draggingPasteboard];
    
    	// Look for paths on the pasteboard.
        if ([[pasteboard types] containsObject:NSFilenamesPboardType]) 
            data = [pasteboard dataForType:NSFilenamesPboardType];
    
        if (data)
    	{
    	[B]NSString *newFolder = @"~/Desktop/";
    	newFolder = [newFolder stringByExpandingTildeInPath];
    	[data writeToFile: newFolder atomically:YES];[/B]
            [COLOR="Red"]//this doesn't seem to work[/COLOR]
    	
    	
    		NSString* errorDescription;
    		
    		// Retrieve  paths.
    		NSArray* filenames = [NSPropertyListSerialization propertyListFromData:data 
    								mutabilityOption:kCFPropertyListImmutable 
    								format:nil 
    								errorDescription:&errorDescription];
    
    	// Add paths to the data source.
    	NSInteger i, n;
    	n = [filenames count];
    	for (i = 0; i < n; i++)
    	{
    	[self addAnImageWithPath:[filenames objectAtIndex:i]];
    	}
    		
    	// Make the image browser reload the data source.
    	[self updateDatasource];
    	}
    
    	// Accept the drag operation.
    	return YES;
    	}
    
     
  4. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    all that makes sense to me, but trying to merge your instructions with apple's ImageBrowser example is very complicated... so it seems...

    with the example i believe there is already an instnace variable for NSString called *path, and those NSStrings are an NSMutableArray called *images... or is it *importedImages (??? - there are 2 arrays in the code)... setting the NSUserDefaults hasn't worked for me, because i think it's only coping the pathname on the pasteboard instead of the actual image... what i'm trying to do is have all images that are dropped into the image browser placed in a specific folder, so when the image browser loads up on launch, the images in the specific folder will display at their proper index. but its complicated because the image browser needs a datasource, which is the path of the images being dropped, and it's the datasource that is indexed... so even if i figure out (eventually - in 8 years) how to force the image browser to load images from a specific folder on launch, they will not be indexed properly...

    *head explodes*
     

Share This Page