Re: Resizing an image inside a text view
Re: Resizing an image inside a text view
- Subject: Re: Resizing an image inside a text view
- From: Keith Blount <email@hidden>
- Date: Mon, 29 Nov 2004 07:01:49 -0800 (PST)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
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.
2) If I drag the image out of the text view and then
drop it back in, the image returns to its original
size.
Does anybody know how I can fix these problems?
If anybody has any suggestions, I would be very
grateful.
Many thanks in advance,
Keith
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden