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

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
I am trying to load an image and display it into an NSImageView. The image is located somewhere in my computer. But I am having some problems. Here is my code.

Code:
-(void)loadImageFromPath:(NSString *)aPath
{
	NSURL *url = [NSURL fileURLWithPath:[aPath stringByStandardizingPath]];
	NSLog([url absoluteString]);
	CIImage *currentImage = [CIImage imageWithContentsOfURL:url];
	if (currentImage == nil) {
		NSLog(@"failed to initialize the image");
	}
	
	mainImage = currentImage;
}

It seems that the image is not initialized. Does anyone know why? I am assuming there is something wrong with my NSUrl methods...
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Wouldn't you have to convert the CIImage to NSImage for the NSImageView?
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
Wouldn't you have to convert the CIImage to NSImage for the NSImageView?
That's not necessary. Apple's examples do not do it this way.

I am enclosing the faulty project. I hope you figure out what I am doing wrong. I am using XCode 3.0, but I don't see any reason this shouldn't work on earlier versions.
 

Attachments

  • Image Filterizer.zip
    37.7 KB · Views: 95

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Well it works for me with a .png file (instead of iTunes.pdf), as long as the line
Code:
//mainImage = [CIImage imageWithContentsOfURL:[NSURL fileURLWithPath:@"~/Desktop/242719UAcy_w.jpg"]];
Is removed.

Though you have no problems in this situation you may need to use retain/release, unless you are using Objective C 2.0, but I know nothing on that.
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
Interesting. Has NSImageView supported CIImage since 10.4, or is this new in 10.5?

/too lazy to look it up ;)

Actually, NSView supported CIImage objects. Tiger definitely had support, since before upgrading, I was looking at Apple's examples and they used an NSView to display CIImage objects.

Since NSImageView is a subclass of NSView, it implements all support for CIImage, too. Oh, and the problem was solved. There was nothing wrong with the code. It just doesn't work with pdf documents.
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
I have another problem right now. When I try to load an image from an arbitrary location, it will crash. It won't tell me why. Can you check it?
 

Attachments

  • Image Filterizer.zip
    41.2 KB · Views: 83

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Actually, NSView supported CIImage objects. Tiger definitely had support, since before upgrading, I was looking at Apple's examples and they used an NSView to display CIImage objects.

Since NSImageView is a subclass of NSView, it implements all support for CIImage, too. Oh, and the problem was solved. There was nothing wrong with the code. It just doesn't work with pdf documents.

Oh, you're just drawing the CIImage directly. I thought you were using the NSImageView setImage: method with a CIImage.

I have another problem right now. When I try to load an image from an arbitrary location, it will crash. It won't tell me why. Can you check it?

Do you have GC enabled? If you do, you are using release/retain unnecessarily. If you don't, you aren't using correct memory management.
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
Do you have GC enabled? If you do, you are using release/retain unnecessarily. If you don't, you aren't using correct memory management.

That makes sense. Although I still cannot find out what the error is. I am not using Carbage Collection.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
That makes sense. Although I still cannot find out what the error is. I am not using Carbage Collection.

Well when you assign mainImage, you don't retain it, and then you use it in drawRect:. So that could be the problem.
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
Well when you assign mainImage, you don't retain it, and then you use it in drawRect:. So that could be the problem.

That solved my problem. So, that produced a question in my tiny brain :)

Why do I need to retain it? When I create the CIImage object, it has a retain count of 1. When does it get released? At the end og the loadImageFromPath: method?

In general, when do autorelease objects get released in cocoa applications? In Foundation tools, they get released when the pool is released. But in Cocoa applications, where are those invisible pools?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
That solved my problem. So, that produced a question in my tiny brain :)

Why do I need to retain it? When I create the CIImage object, it has a retain count of 1. When does it get released? At the end og the loadImageFromPath: method?

In general, when do autorelease objects get released in cocoa applications? In Foundation tools, they get released when the pool is released. But in Cocoa applications, where are those invisible pools?

In Cocoa there is an autorelease pool automatically in place for the main thread (you need to create your own for other threads). This is flushed at the end of each run loop. Object created by convenience methods (class level methods that create an object in one call) are auto-released. Objects created by alloc/init are not.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.