Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Adding NSImage to NSMutableDictionary

DonkeyNoseDonkeyNose Posts: 15New Users *
edited May 2013 in Mac OS X Development
Hello,

I have more than a dozen iOS application that utilize NSMutableDictionary to keep data. Now, I'm working on a Mac OS X app. What I want to do with NSMutableDictionary is store (image) file information (image, name, path...) so that the application can list it with NSTableView. Anyway, I have the following code.

- (void)addAnImageWithPath:(NSString *)path { key1a = [NSImage imageNamed:NSImageNameUserAccounts]; key1b = @"KEY1"; // filename key1c = @"KEY2"; // filepath key1d = @"KEY3"; // dimensions key1e = @"KEY4"; // validation (file exists) NSString *fileExtension = [path pathExtension]; NSString *fileName = [path lastPathComponent]; if ([fileExtension.lowercaseString isEqualToString:@"bmp"] || [fileExtension.lowercaseString isEqualToString:@"gif"] ||[fileExtension.lowercaseString isEqualToString:@"jpg"] || [fileExtension.lowercaseString isEqualToString:@"jpeg"] || [fileExtension.lowercaseString isEqualToString:@"png"] || [fileExtension.lowercaseString isEqualToString:@"pdf"] || [fileExtension.lowercaseString isEqualToString:@"tif"] || [fileExtension.lowercaseString isEqualToString:@"tiff"] || [fileExtension.lowercaseString isEqualToString:@"psd"]) { NSImage *image0 = [[NSImage alloc] initWithContentsOfFile:path]; NSString *dim = [NSString stringWithFormat:@"%0.0f x %0.0f",image0.size.width,image0.size.height]; dict1 = [NSDictionary dictionaryWithObjectsAndKeys:image0,key1a,fileName,key1b,path,key1c,dim,key1d,@"1",key1e,nil]; [itemArray1 addObject:dict1]; [tableview reloadData]; } }

This method will get called when the user clicks on a toolbar button to select one or more files. I use pretty much the same code as above with iOS applications except that key1a is NSImage as opposed to UIImage. To make the story short, I've figured out that the application adds a blank image to key1a. Do you know what I'm doing wrong? Other keys do hold data. By the way, I'm assuming that NSImage is equivalent to UIImage. So the table will be populated with data according except an image.

Thank you for your help.

Replies

  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,692Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    What do you mean by "a blank image"?

    As for the specifics of what you are trying, note the documentation:
    Toolbar Named Images
    Images that you can use in application toolbars. To access this image, pass the specified constant to the imageNamed: method.

    NSString *const NSImageNameUserAccounts;
    NSString *const NSImageNamePreferencesGeneral;
    NSString *const NSImageNameAdvanced;
    NSString *const NSImageNameInfo;
    NSString *const NSImageNameFontPanel;
    NSString *const NSImageNameColorPanel;
    Constants
    NSImageNameUserAccounts
    User account toolbar icon. Use in a preferences window only.
    So it would seem you are not using this properly.

    And then your file extension checking is a mess. Any time you have that much repeated code, you're doing something wrong.

    First of all, lowercaseString is a method, not a property, so the use of dot syntax here is not appropriate. So even if you do nothing else, this one simple change:
    NSString *fileExtension = [[path pathExtension] lowercaseString];
    ...
    if ([fileExtension isEqualToString:@"bmp"] || [fileExtension isEqualToString:@"gif"] ||[fileExtension isEqualToString:@"jpg"]...
    ...improves things tremendously. But it is still too much code. Consider:
    NSSet *supportedFileTypes = [NSSet setWithObjects:@"bmp", @"gif", @"jpg", @"jpeg", @"png", @"pdf", @"tif", @"tiff", @"psd", nil];
    if ([supportedFileTypes containsObject:fileExtension])
    {
       // Populate the dictionary
    }
    Ahhhh, so clean and nice.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | SlickShopper 2 | Leave a PayPal donation
  • DonkeyNoseDonkeyNose Posts: 15New Users *

    What do you mean by "a blank image"?

    As for the specifics of what you are trying, note the documentation:

    Toolbar Named Images
    Images that you can use in application toolbars. To access this image, pass the specified constant to the imageNamed: method.

    NSString *const NSImageNameUserAccounts;
    NSString *const NSImageNamePreferencesGeneral;
    NSString *const NSImageNameAdvanced;
    NSString *const NSImageNameInfo;
    NSString *const NSImageNameFontPanel;
    NSString *const NSImageNameColorPanel;
    Constants
    NSImageNameUserAccounts
    User account toolbar icon. Use in a preferences window only.
    So it would seem you are not using this properly.

    And then your file extension checking is a mess. Any time you have that much repeated code, you're doing something wrong.

    First of all, lowercaseString is a method, not a property, so the use of dot syntax here is not appropriate. So even if you do nothing else, this one simple change:
    NSString *fileExtension = [[path pathExtension] lowercaseString];
    ...
    if ([fileExtension isEqualToString:@"bmp"] || [fileExtension isEqualToString:@"gif"] ||[fileExtension isEqualToString:@"jpg"]...
    ...improves things tremendously. But it is still too much code. Consider:
    NSSet *supportedFileTypes = [NSSet setWithObjects:@"bmp", @"gif", @"jpg", @"jpeg", @"png", @"pdf", @"tif", @"tiff", @"psd", nil];
    if ([supportedFileTypes containsObject:fileExtension])
    {
       // Populate the dictionary
    }
    Ahhhh, so clean and nice.



    Thanks. My question is about adding NSImage to NSDictionary, though. 'Blank' means there's something in the key in question. If not, the application could crash at least under iOS. Nonetheless, it's not visible. Also, with your manner, the application would reject files with an extension of JPG.
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,692Tutorial Authors, Registered Users @ @ @ @ @ @ @ @

    Thanks. My question is about adding NSImage to NSDictionary, though. 'Blank' means there's something in the key in question. If not, the application could crash at least under iOS. Nonetheless, it's not visible.

    Do a test and provide that image directly to an image view. Documentation makes it sound like you can't use it the way you are wanting to. If there is something in the dictionary for a key, and that something is an image object, then perhaps you don't have your table view wired up correctly.

    Also, with your manner, the application would reject files with an extension of JPG.

    Nope.
    NSString *fileExtension = [[path pathExtension] lowercaseString];
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | SlickShopper 2 | Leave a PayPal donation
  • DonkeyNoseDonkeyNose Posts: 15New Users *

    Do a test and provide that image directly to an image view.

    I've already done that. That's why I've said earlier "I've figured out that the application adds a blank image to key1a."

    I'm done with this topic here. I'll ask somewhere else.
Sign In or Register to comment.