how to set datasource as image files in specific folder, and load datasource on launch?
how to set datasource as image files in specific folder, and load datasource on launch?
- Subject: how to set datasource as image files in specific folder, and load datasource on launch?
- From: "Chunk 1978" <email@hidden>
- Date: Wed, 5 Nov 2008 17:22:27 -0500
i'm trying to figure out how to set the image browser to a specific
directory of images and load those images with [update datasource] in
the awakeFromNib method... currently, only drag and drop is possible,
and the datasource is set to the dropped object's path, but i'd like
to save all images dropped into the image browser into it's own
specific folder and save the reordering of those images with
NSUserDefaults...
#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>
@class IKImageBrowserView;
@interface SwatchController : NSObject
{
IBOutlet IKImageBrowserView *imageBrowser;
NSMutableArray *images;
NSMutableArray *importedImages;
}
@end
#import "SwatchController.h"
@interface myImageObject : NSObject
{
NSString* path;
}
@end
@implementation myImageObject
- (void)dealloc
{
[path release];
[super dealloc];
}
- (void)setPath:(NSString*)inPath
{
if (path != inPath)
{
[path release];
path = [inPath retain];
}
}
#pragma mark Item Datasource Protocol
- (NSString*)imageRepresentationType
{
return IKImageBrowserPathRepresentationType;
}
- (id)imageRepresentation
{
return path;
}
- (NSString*)imageUID
{
return path;
}
@end
@implementation SwatchController
- (void)dealloc
{
[images release];
[importedImages release];
[super dealloc];
}
- (void)awakeFromNib
{
images = [[NSMutableArray alloc] init];
importedImages = [[NSMutableArray alloc] init];
[imageBrowser setAllowsReordering:YES];
[imageBrowser setAnimates:YES];
[imageBrowser setDraggingDestinationDelegate:self];
}
#pragma mark Import Images From File System
- (BOOL)isImageFile:(NSString*)filePath
{
BOOL isImageFile = NO;
LSItemInfoRecord info;
CFStringRef uti = NULL;
CFURLRef url = CFURLCreateWithFileSystemPath(NULL,
(CFStringRef)filePath, kCFURLPOSIXPathStyle, FALSE);
if (LSCopyItemInfoForURL(url, kLSRequestExtension |
kLSRequestTypeCreator, &info) == noErr)
{
if (info.extension != NULL)
{
uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
info.extension, kUTTypeData);
CFRelease(info.extension);
}
if (uti == NULL)
{
CFStringRef typeString = UTCreateStringForOSType(info.filetype);
if ( typeString != NULL)
{
uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassOSType,
typeString, kUTTypeData);
CFRelease(typeString);
}
}
if (uti != NULL)
{
CFArrayRef supportedTypes = CGImageSourceCopyTypeIdentifiers();
CFIndex i, typeCount = CFArrayGetCount(supportedTypes);
for (i = 0; i < typeCount; i++)
{
if (UTTypeConformsTo(uti,
(CFStringRef)CFArrayGetValueAtIndex(supportedTypes, i)))
{
isImageFile = YES;
break;
}
}
}
}
return isImageFile;
}
- (void)addAnImageWithPath:(NSString*)path
{
BOOL addObject = NO;
NSDictionary* fileAttribs = [[NSFileManager defaultManager]
fileAttributesAtPath:path traverseLink:YES];
if (fileAttribs)
{
if ([NSFileTypeDirectory isEqualTo:[fileAttribs objectForKey:NSFileType]])
{
if ([[NSWorkspace sharedWorkspace] isFilePackageAtPath:path] == NO)
{
addObject = YES;
}
}
else
{
addObject = YES;
}
}
if (addObject && [self isImageFile:path])
{
myImageObject* p = [[myImageObject alloc] init];
[p setPath:path];
[importedImages addObject:p];
[p release];
}
}
#pragma mark IKImageBrowser DataSource
- (int)numberOfItemsInImageBrowser:(IKImageBrowserView*)view
{
return [images count];
}
- (id)imageBrowser:(IKImageBrowserView *) view itemAtIndex:(int) index
{
return [images objectAtIndex:index];
}
- (void)imageBrowser:(IKImageBrowserView*)view removeItemsAtIndexes:
(NSIndexSet*)indexes
{
[images removeObjectsAtIndexes:indexes];
}
- (BOOL)imageBrowser:(IKImageBrowserView*)view moveItemsAtIndexes:
(NSIndexSet*)indexes toIndex:(unsigned int)destinationIndex
{
NSInteger index;
NSMutableArray *temporaryArray;
temporaryArray = [[[NSMutableArray alloc] init] autorelease];
for (index = [indexes lastIndex]; index != NSNotFound; index =
[indexes indexLessThanIndex:index])
{
if (index < destinationIndex)
{
destinationIndex --;
}
id obj = [images objectAtIndex:index];
[temporaryArray addObject:obj];
[images removeObjectAtIndex:index];
}
NSInteger n = [temporaryArray count];
for (index = 0; index < n; index++)
{
[images insertObject:[temporaryArray objectAtIndex:index]
atIndex:destinationIndex];
}
return YES;
}
- (void)updateDatasource
{
[images addObjectsFromArray:importedImages];
[importedImages removeAllObjects];
[imageBrowser reloadData];
}
#pragma mark Drag & Drop
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
return NSDragOperationCopy;
}
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
return NSDragOperationCopy;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
NSData *data = nil;
NSPasteboard *pasteboard = [sender draggingPasteboard];
if ([[pasteboard types] containsObject:NSFilenamesPboardType])
{
data = [pasteboard dataForType:NSFilenamesPboardType];
}
if (data)
{
NSString* errorDescription;
NSArray* filenames = [NSPropertyListSerialization
propertyListFromData:data mutabilityOption:kCFPropertyListImmutable
format:nil errorDescription:&errorDescription];
NSInteger i, n;
n = [filenames count];
for (i = 0; i < n; i++)
{
[self addAnImageWithPath:[filenames objectAtIndex:i]];
}
[self updateDatasource];
}
return YES;
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden