Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

blackend.dk

macrumors newbie
Original poster
Jan 16, 2012
4
0
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 ?
 
Last edited by a moderator:
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"];
   
    }
}
 
Last edited by a moderator:
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.
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.