FROM : Ryan Stevens
DATE : Mon Nov 29 18:23:38 2004
On Nov 29, 2004, at 7:01 AM, Keith Blount wrote:
> Sorry to reply to my own thread, but I have been
> experimenting more with trying to resize an image
> within an NSTextView. Basically I want to allow the
> user to resize an image he or she has dragged into the
> text view - just as you can in Word, Mellel or Nisus -
> probably by ctrl-clicking on the pic to bring up a
> slider that will scale the image.
>
> I have written some very basic test code in an attempt
> to do this. Currently, the code does this: it assumes
> that an image has been put into a text view as the
> first character. If I click on a "Resize Image"
> button, a resizeImage: method is called that resizes
> said image to 200 x 200, by accessing the image from
> the text attachment cell and resizing it. Here is the
> code I am using:
>
> // My initial test code to resize an image within the
> text view
> - (IBAction)resizeImage:(id)sender
> {
> NSTextAttachment *attachment = [[textView
> textStorage] attribute:NSAttachmentAttributeName
> atIndex:0
> effectiveRange:nil];
> NSTextAttachmentCell *cell = [attachment
> attachmentCell];
> NSImage *image = [cell image];
> [self imageResize:image
> newSize:NSMakeSize(200.0,200.0)]; // A random test
> size
> [cell setImage:image];
> }
>
> // This is Lorenzo's code to resize an image from
> another thread on this list
> - (void)imageResize:(NSImage*)anImage
> newSize:(NSSize)newSize
> {
> NSImage *workImage = [[NSImage alloc]
> initWithSize:newSize];
>
> NSSize oldSize = [anImage size];
> NSRect sourceRect = NSMakeRect(0, 0,
> oldSize.width, oldSize.height);
> NSRect destRect = NSMakeRect(0, 0, newSize.width,
> newSize.height);
>
> [anImage setScalesWhenResized:YES];
> [anImage setSize:newSize];
> [workImage lockFocus];
> [anImage drawInRect:destRect fromRect:sourceRect
> operation:NSCompositeCopy fraction:1.0];
> [workImage unlockFocus];
>
> [workImage release];
> }
>
> On a very basic level, this does actually work - the
> image in the text view IS resized. This is a start.
> But here are the problems I now have:
>
> 1) The changes to the image aren't visible until you
> start typing in the text view. Even if I call
> setNeedsDisplay: on the textView, the image isn't
> displayed properly until I enter a return.
>
You might need to use the layoutManager..
[[yourTextView layoutManager]
invalidateDisplayForCharacterRange:dirtyRange];
then setNeedsDisplay? Just a guess.
> 2) If I drag the image out of the text view and then
> drop it back in, the image returns to its original
> size.
Not sure what's going on here.
Anyway, I would probably use an NSTexAttachmentCell subclass for
drawing. This way you can override
trackMouse:inRect:ofView:untilMouseUp: (guessing again) to figure out
when to resize the image. You could also add a setCellSize: method to
the subclass so the image is always the normal size - it's just the
cellSize that changes.
HTH!
DATE : Mon Nov 29 18:23:38 2004
On Nov 29, 2004, at 7:01 AM, Keith Blount wrote:
> Sorry to reply to my own thread, but I have been
> experimenting more with trying to resize an image
> within an NSTextView. Basically I want to allow the
> user to resize an image he or she has dragged into the
> text view - just as you can in Word, Mellel or Nisus -
> probably by ctrl-clicking on the pic to bring up a
> slider that will scale the image.
>
> I have written some very basic test code in an attempt
> to do this. Currently, the code does this: it assumes
> that an image has been put into a text view as the
> first character. If I click on a "Resize Image"
> button, a resizeImage: method is called that resizes
> said image to 200 x 200, by accessing the image from
> the text attachment cell and resizing it. Here is the
> code I am using:
>
> // My initial test code to resize an image within the
> text view
> - (IBAction)resizeImage:(id)sender
> {
> NSTextAttachment *attachment = [[textView
> textStorage] attribute:NSAttachmentAttributeName
> atIndex:0
> effectiveRange:nil];
> NSTextAttachmentCell *cell = [attachment
> attachmentCell];
> NSImage *image = [cell image];
> [self imageResize:image
> newSize:NSMakeSize(200.0,200.0)]; // A random test
> size
> [cell setImage:image];
> }
>
> // This is Lorenzo's code to resize an image from
> another thread on this list
> - (void)imageResize:(NSImage*)anImage
> newSize:(NSSize)newSize
> {
> NSImage *workImage = [[NSImage alloc]
> initWithSize:newSize];
>
> NSSize oldSize = [anImage size];
> NSRect sourceRect = NSMakeRect(0, 0,
> oldSize.width, oldSize.height);
> NSRect destRect = NSMakeRect(0, 0, newSize.width,
> newSize.height);
>
> [anImage setScalesWhenResized:YES];
> [anImage setSize:newSize];
> [workImage lockFocus];
> [anImage drawInRect:destRect fromRect:sourceRect
> operation:NSCompositeCopy fraction:1.0];
> [workImage unlockFocus];
>
> [workImage release];
> }
>
> On a very basic level, this does actually work - the
> image in the text view IS resized. This is a start.
> But here are the problems I now have:
>
> 1) The changes to the image aren't visible until you
> start typing in the text view. Even if I call
> setNeedsDisplay: on the textView, the image isn't
> displayed properly until I enter a return.
>
You might need to use the layoutManager..
[[yourTextView layoutManager]
invalidateDisplayForCharacterRange:dirtyRange];
then setNeedsDisplay? Just a guess.
> 2) If I drag the image out of the text view and then
> drop it back in, the image returns to its original
> size.
Not sure what's going on here.
Anyway, I would probably use an NSTexAttachmentCell subclass for
drawing. This way you can override
trackMouse:inRect:ofView:untilMouseUp: (guessing again) to figure out
when to resize the image. You could also add a setCellSize: method to
the subclass so the image is always the normal size - it's just the
cellSize that changes.
HTH!
| Related mails | Author | Date |
|---|---|---|
| Keith Blount | Nov 29, 01:17 | |
| Keith Blount | Nov 29, 16:01 | |
| heinrich.giesen | Nov 29, 17:59 | |
| Ryan Stevens | Nov 29, 18:23 | |
| Keith Blount | Nov 29, 21:10 |






Cocoa mail archive

