NSImage and NSImageView Problems

Discussion in 'Mac Programming' started by Soulstorm, Dec 18, 2006.

  1. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    I have decided to make an application in Cocoa. The application will an image file, and will just add a text on top of it at the selected location.

    I have managed to display the image using an NSImageView, and I have managed to display a selected text onto the image. I also wrote a very basic export feature (I will fix them all better afterwards).

    But I have some problems:
    1)I initially wanted to implement an NSTextView to allow the user to give the text he/shw wants and to add multiple lines of text. I couldn't do because every time I tried etracting the NSString from it (using the -string method) it gave me a nil string and the debugger came up. So I used an NSTextField instead. What can I do to fix that?
    2)Most importantly: I want to find a way to resize large images. For smaller images, I have written code that resizes the image to fit the NSImageView. But that doesn't work for larger images. How can I scale them down to fit the NSImageView?
    3)Last but not least, for large images, I want to be able to scroll the NSImageView so that I can see every inch of the image it displays. The way it is now, I can only see a small portion of the image that I am displaying.

    If anyone could help me with these problems it would be greatly appreciated. I am attaching my project so far. I am warning you: It is my first project with images.
     

    Attached Files:

  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    1) It's stringValue not string.
     
  3. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #3
    NSTextView does not have a member named -stringValue, because it does not inherit from NSControl. Instead, it inherits from NSText, which contains a function named -string.
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Guess I should RTFM more then! I just assumed it was an NSControl somewhere down the line.

    I'm on a Windows PC at work so I can't do much with your code here: I'll look at it when I'm at home...
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    See attached

    1) Click on the NSImageView in IB and choose Layout->Make subview of->Scroll view

    2) Click on the scroll view, shift-command-I and make both inner springs springy (auto-resize)

    3) See the addition of setFrame to loadImageFromPath:

    4) Replaced the text field with a text view. Works fine?

    General comments:

    Your design is all wrong. Your custom view is acting as a controller. You need to think MVC more...
     

    Attached Files:

  6. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #6
    Actually it gets me a minor error. During your absence I fixed some of my problems. See what I have done.

    EDIT: Fixed the link.
     

    Attached Files:

  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    Where?

    Edit: got it!
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Changing the frame size inside drawRect: is a really bad idea. This could cause dead-locks and crashes. My solution of setting the frame size when the image was loaded is better.
     
  9. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #9
    You are right. I will change it now. Thanks robbie for taking the time to look at my code.
     

Share This Page