PDA

View Full Version : NSimage from file




blackend.dk
Jan 16, 2012, 05:04 AM
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.....

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...

- (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 ?



Mr. Retrofire
Jan 16, 2012, 05:12 AM
Are you sure, you use the correct reference type for the image file(s)?

blackend.dk
Jan 16, 2012, 06:51 AM
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..

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"];

}
}

gnasher729
Jan 16, 2012, 07:23 AM
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.

blackend.dk
Jan 16, 2012, 09:12 AM
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