Re: Displaying jpeg images (from HD)
Re: Displaying jpeg images (from HD)
- Subject: Re: Displaying jpeg images (from HD)
- From: Clyde McQueen <email@hidden>
- Date: Sun, 10 Jun 2001 11:38:43 -0700
On Sunday, June 10, 2001, at 01:16 AM, Giovanni A. D. wrote:
My question is : do you have a sample code that explain how to load
a picture from the hard disk using a NSOpenPanel (<- i hope that's
right :
) ?
Method 1: if you set up a document app, and set your document type to
"jpg", then you can load it using the standard document code generated
by PB:
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
// Insert code here to read your document from the given data.
// You can also choose to override
-loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead.
// oBits is an instance variable in the document.
[oBits release];
oBits = [[NSBitmapImageRep imageRepWith
Data:data] retain];
return YES;
}
Method 2: ignore the document architecture, and just open the file. This
is a snippet of code that imports a series of jpegs:
- (IBAction)actionImportPictures:(id)sender
{
int result;
NSMutableArray *fileTypes = [NSArray array];
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
// Add the various picture types we support.
// In Cocoa file names are case sensitive, so include lots of options.
// Also include the old Mac 'JPEG' file type.
[fileTypes addObject:@"jpg"];
[fileTypes addObject:@"JPG"];
[fileTypes addObject:@"jpeg"];
[fileTypes addObject:@"JPEG"];
[fileTypes addObject:@"'JPEG'"]; // This is interpreted as an old
Mac 4-char code.
// Allow multiple selection.
[oPanel setAllowsMultipleSelection:YES];
result = [oPanel runModalForDirectory:NSHomeDirectory() file:nil
types:fileTypes];
if (result == NSOKButton) {
// Get the list of files.
NSArray *filesToOpen = [oPanel filenames];
int count = [filesToOpen count];
// Build up an array of pictures.
NSMutableArray *newPictures = [NSMutableArray
arrayWithCapacity:count];
int i;
for (i = 0; i < count; i++) {
[newPictures addObject:[NSBitmapImageRep
imageRepWithContentsOfFile:[filesToOpen objectAtIndex:i]]];
}
... now do something with newPictures ...
}
}
/Clyde