CCImage Initialization Problems

Discussion in 'Mac Programming' started by Soulstorm, Nov 4, 2007.

  1. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #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...
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    That looks like it should work. Can you give us an example aPath that does not work?
     
  3. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    Wouldn't you have to convert the CIImage to NSImage for the NSImageView?
     
  4. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #4
    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.
     

    Attached Files:

  5. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    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.
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    Interesting. Has NSImageView supported CIImage since 10.4, or is this new in 10.5?

    /too lazy to look it up ;)
     
  7. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #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.
     
  8. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #8
    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?
     

    Attached Files:

  9. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    Oh, you're just drawing the CIImage directly. I thought you were using the NSImageView setImage: method with a CIImage.

    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.
     
  10. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #10
    That makes sense. Although I still cannot find out what the error is. I am not using Carbage Collection.
     
  11. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #11
    Well when you assign mainImage, you don't retain it, and then you use it in drawRect:. So that could be the problem.
     
  12. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #12
    As I mentioned before ;).
     
  13. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #13
    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?
     
  14. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #14
    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.
     

Share This Page