Re: NSImage resizing
Re: NSImage resizing
- Subject: Re: NSImage resizing
- From: Steve Christensen <email@hidden>
- Date: Fri, 09 Jan 2009 08:55:42 -0800
For a bitmap image, -setSize: effectively alters the DPI of the pixels
but doesn't actually create a new bitmap of a different size. So for a
72dpi image, the NSBitmapImageRep's -width and -height methods should
give you the same values as for the image's -size method. But for a
300dpi image, for example, you'd expect the -width and -height values
to be about 4x larger than the -size value.
Maybe something like this would do what you want?
NSImage* originalIcon = [[NSWorkspace sharedWorkspace]
iconForFile:filePath];
NSRect resizedBounds = NSMakeRect(0, 0, 384, 384);
NSImage* resizedIcon = [[[NSImage alloc] resizedBounds.size]
autorelease];
[resizedIcon lockFocus];
[originalIcon drawInRect:resizedBounds fromRect:NSZeroRect
operation:NSCompositeCopy fraction:1.0];
[resizedIcon unlockFocus];
NSBitmapImageRep* bitmapImage = [[resizedIcon representations]
objectAtIndex:0];
[[bitmapImage representationUsingType:NSJPEGFileType properties:dict]
writeToFile:destPath atomically:YES];
Probably safer to iterate the representation(s) and actually add a
check to see that bitmapImage really is one before actually using it,
just in case another representation is created.
steve
On Jan 9, 2009, at 5:50 AM, Parimal Das wrote:
hi
in my application i need to resize a .jpg image through a command line
my code is
NSImage *icon = [[NSWorkspace sharedWorkspace]
iconForFile:filePath]; // get
icon from the file at filePath destination
NSSize imageSize;
imageSize.width = 384.0; // in points (384 pts = 512 px )
imageSize.height = 384.0;
[icon setSize:imageSize]; // set image size
NSData * tiffData = [icon TIFFRepresentation];
bitmapImageRep = [NSBitmapImageRep imageRepWithData:tiffData];
[[bitmapImageRep representationUsingType:NSJPEGFileType
properties:dict]
writeToFile:destPath atomically:YES]; // write it to a file path
stored in
destPath
this code is not altering the size at all
i cant create a NSView as its a command line app
any suggestions ??? how to resize it??
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden