我有NSBitmap
ImageRep是WxH大小.我创建NSImage并做addRepresentation:.然后我需要调整NSImage的大小.我尝试了setSize方法,但它不起作用.我该怎么办?
解决方法
因为Paresh的方法是完全正确的,而是自10.8以来弃用,我将在下面发布工作的10.8代码.所有这一切都归功于Paresh的答案.
- (NSImage *)imageResize:(NSImage*)anImage newSize:(NSSize)newSize { NSImage *sourceImage = anImage; [sourceImage setScalesWhenResized:YES]; // Report an error if the source isn't a valid image if (![sourceImage isValid]){ NSLog(@"Invalid Image"); } else { NSImage *smallImage = [[NSImage alloc] initWithSize: newSize]; [smallImage lockFocus]; [sourceImage setSize: newSize]; [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh]; [sourceImage drawAtPoint:NSZeroPoint fromRect:CGRectMake(0,newSize.width,newSize.height) operation:NSCompositeCopy fraction:1.0]; [smallImage unlockFocus]; return smallImage; } return nil; }