PDA

View Full Version : CCImage Initialization Problems




Soulstorm
Nov 4, 2007, 04:25 AM
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.

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



robbieduncan
Nov 4, 2007, 04:37 AM
That looks like it should work. Can you give us an example aPath that does not work?

kainjow
Nov 4, 2007, 07:20 AM
Wouldn't you have to convert the CIImage to NSImage for the NSImageView?

Soulstorm
Nov 4, 2007, 02:07 PM
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.

Eraserhead
Nov 4, 2007, 02:29 PM
Well it works for me with a .png file (instead of iTunes.pdf), as long as the line

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

kainjow
Nov 4, 2007, 09:39 PM
That's not necessary. Apple's examples do not do it this way.

Interesting. Has NSImageView supported CIImage since 10.4, or is this new in 10.5?

/too lazy to look it up ;)

Soulstorm
Nov 5, 2007, 02:56 AM
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
Nov 5, 2007, 11:43 AM
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?

kainjow
Nov 5, 2007, 12:45 PM
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
Nov 5, 2007, 01:02 PM
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
Nov 5, 2007, 01:08 PM
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.

Eraserhead
Nov 5, 2007, 01:22 PM
Well when you assign mainImage, you don't retain it, and then you use it in drawRect:. So that could be the problem.

As I mentioned before ;).

Soulstorm
Nov 5, 2007, 03:58 PM
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
Nov 5, 2007, 05:32 PM
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.