NSimage from file

Discussion in 'Mac Programming' started by blackend.dk, Jan 16, 2012.

  1. blackend.dk, Jan 16, 2012
    Last edited by a moderator: Jan 16, 2012

    blackend.dk
    Expand Collapse
    macrumors newbie

    Joined:
    Jan 16, 2012
    #1
    Hi group
    I'm new to Cocoa and Objective-C. Not to programming though.

    I'm trying to get/set an image from file (and core) data.

    If i use the opendialog it dosent work.....

    Code:
            for( i = 0; i < [files count]; i++ )
            {
                NSString* fileName = [files objectAtIndex:i];
                
                // Do something with the filename.
                [_txtFileName setStringValue:fileName];
                
            }
            NSImage *tempImage = [[NSImage alloc] initWithContentsOfFile:[_txtFileName stringValue]];
            
            // set the image in the image view
            if (tempImage != nil){
                NSLog(@"Picture found");
                [_imageVi setImage: tempImage];
            }
            else
                NSLog(@"Ingen billede");
        }
    
                
            }
    _txtFileName is a textfield linked to core data.


    But if i "hard code" it works...

    Code:
    - (IBAction)changepicture:(id)sender {
        NSImage *tempImage = [[NSImage alloc] initWithContentsOfFile:@"/Users/Hansen/Pictures/Apple.jpg"];
        
        // set the image in the image view
        if (tempImage != nil){
            NSLog(@"Picture found");
            [_imageVi setImage: tempImage];
        }
    }
    
    What am i doing wrong ?
     
  2. Mr. Retrofire
    Expand Collapse
    macrumors 601

    Mr. Retrofire

    Joined:
    Mar 2, 2010
    Location:
    www.emiliana.cl
    #2
    Are you sure, you use the correct reference type for the image file(s)?
     
  3. blackend.dk, Jan 16, 2012
    Last edited by a moderator: Jan 16, 2012

    blackend.dk
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    Jan 16, 2012
    #3
    I think so yes. I have copied the path form the NSTextField (_txtFileName)from the first solution/try, so the path sould be exatly the same...

    I'm trying another approach though - this seems to work, except i'm inserting a new post, instead i want to edit the "imagepath"
    So I need to figure that out..
    Code:
        NSMutableArray *allowedFile = [[NSMutableArray alloc] init];
        [allowedFile addObject:@"jpg"];
        [allowedFile addObject:@"png"];
        [allowedFile addObject:@"gif"];
        
        NSOpenPanel *tvarNSOpenPanelObj = [NSOpenPanel openPanel];
        
        NSInteger tvarNSInteger = [tvarNSOpenPanelObj runModalForTypes:allowedFile];
        
        if(tvarNSInteger == NSOKButton){
            NSImage *theImage = [[NSImage alloc] initWithContentsOfFile: [tvarNSOpenPanelObj filename]];
            [_imageVi setImage: theImage];   
            
            NSManagedObjectContext *context = [self managedObjectContext];
            NSManagedObject *places = [NSEntityDescription 
                                            insertNewObjectForEntityForName:@"Place" 
                                            inManagedObjectContext:context];
            [places setValue:[tvarNSOpenPanelObj filename] forKey:@"imagepath"];
       
        }
    }
    
     
  4. gnasher729
    Expand Collapse
    macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    What's the type of [files objectAtIndex:i]? Is it NSString*? Are you sure? Considering that your code doesn't work, are you still sure? An NSLog statement will tell you for sure.

    What's the value of [_txtFileName stringValue] when you use it to load an image? Is it a valid path? Are you sure? Considering that your code doesn't work, are you still sure? An NSLog statement will tell you for sure.
     
  5. blackend.dk
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    Jan 16, 2012
    #5
    Hi gnasher729

    Well, i guess i better take another approach on my application.

    I think the correct way for me, is not to edit the image at once.
    Instead i have to edit my core data and then update the image according to the data.

    This is all new to me, so i have to read up on accessing, editing and saving core data code via class.

    But thanks anyway :-D
     

Share This Page