Changing NSImage resolution - or display it at pixel size
Changing NSImage resolution - or display it at pixel size
- Subject: Changing NSImage resolution - or display it at pixel size
- From: Jeremy Dronfield <email@hidden>
- Date: Mon, 3 May 2004 21:39:17 +0100
I've been struggling with this problem for two days, and haven't found
anything in the archives to solve it. How do you change the resolution
of an NSImage? And if that isn't possible (it must be), is there a way
to force an NSTextView to display images in attachments at their pixel
size instead of dpi?
Below is the code I'm using to (try to) change the resolution of a jpg
image (200 dpi) containing one NSBitmapImageRep in an
NSTextAttachmentCell:
if ([(NSCell *)[imAttachment attachmentCell]
respondsToSelector:@selector(image)]) {
NSImage *theImage = [[(NSCell *)[imAttachment attachmentCell] image]
copy];
NSLog(@"initial image:%@", [theImage description]);
NSBitmapImageRep *rep = [[theImage representations] objectAtIndex:0];
int renderedWidth = [rep pixelsWide];
int renderedHeight = [rep pixelsHigh];
[rep setSize:NSMakeSize(renderedWidth, renderedHeight)];
[theImage setSize:NSMakeSize(renderedWidth, renderedHeight)];
[theImage addRepresentation:rep];
[theImage removeRepresentation:[[theImage representations]
objectAtIndex:0]];
if ([(NSCell *)[imAttachment attachmentCell]
respondsToSelector:@selector(setImage:)])
[(NSCell *)[imAttachment attachmentCell] setImage:theImage];
NSLog(@"adjusted image:%@", [theImage description]);
[theImage release];
}
At first, this appears to work. The image in the text appears at its
pixel size. This is borne out by the log output:
initial image:NSImage 0x3e8c10 Size={130.32, 94} Reps=(
NSBitmapImageRep 0x3e8c30 Size={130.32, 94}
ColorSpace=NSCalibratedRGBColorSpace BPS=8 Pixels=362x261 Alpha=NO
adjusted image:NSImage 0x3e8c10 Size={362, 261} Reps=(
NSBitmapImageRep 0x3e8c30 Size={362, 261}
ColorSpace=NSCalibratedRGBColorSpace BPS=8 Pixels=362x261 Alpha=NO
However, when the text view's contents are saved to RTFD and reloaded
later, the image has slipped back to its original size. Why does this
happen? Is there a better way to change the image's resolution? Or
preferably, is there a way simply to get NSTextView to show images at
their pixel size regardless of dpi?
Regards
-Jeremy
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.