PDA

View Full Version : NSImage and NSImageView Problems




Soulstorm
Dec 18, 2006, 06:35 AM
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.



robbieduncan
Dec 18, 2006, 06:53 AM
1) It's stringValue not string.

Soulstorm
Dec 18, 2006, 06:58 AM
1) It's stringValue not string.

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.

robbieduncan
Dec 18, 2006, 07:10 AM
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.

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

robbieduncan
Dec 18, 2006, 01:08 PM
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...

Soulstorm
Dec 18, 2006, 01:55 PM
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.

robbieduncan
Dec 18, 2006, 01:57 PM
Actually it gets me a minor error. During your absence I fixed some of my problems. See what I have done.

Where?

Edit: got it!

robbieduncan
Dec 18, 2006, 02:04 PM
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.

Soulstorm
Dec 18, 2006, 02:18 PM
You are right. I will change it now. Thanks robbie for taking the time to look at my code.