首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >扩展NSImage并节省成本

扩展NSImage并节省成本
EN

Stack Overflow用户
提问于 2013-03-23 21:49:57
回答 1查看 5K关注 0票数 2

我想把64px的图像放大到512px (即使它是模糊的或像素化的)

我使用它从我的NSImageView中获取图像并保存:

代码语言:javascript
运行
复制
NSData *customimageData = [[customIcon image] TIFFRepresentation];
    NSBitmapImageRep *customimageRep = [NSBitmapImageRep imageRepWithData:customimageData];


    customimageData = [customimageRep representationUsingType:NSPNGFileType properties:nil];



    NSString* customBundlePath = [[NSBundle mainBundle] pathForResource:@"customIcon" ofType:@"png"];
    [customimageData writeToFile:customBundlePath atomically:YES];

我试过setSize:但还是节省了64px。

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-24 00:18:31

不能使用NSImage的size属性,因为它只与图像表示的像素大小有间接关系。调整像素大小的一个好方法是使用NSImageRepdrawInRect方法:

代码语言:javascript
运行
复制
 - (BOOL)drawInRect:(NSRect)rect

在指定的矩形中绘制整个图像,并根据需要对其进行缩放。

这是一个调整图像大小的方法(以你想要的像素大小创建一个新的NSImage )。

代码语言:javascript
运行
复制
- (NSImage*) resizeImage:(NSImage*)sourceImage size:(NSSize)size
{

    NSRect targetFrame = NSMakeRect(0, 0, size.width, size.height);     
    NSImage* targetImage = nil;
    NSImageRep *sourceImageRep =
    [sourceImage bestRepresentationForRect:targetFrame
                                   context:nil
                                     hints:nil];

    targetImage = [[NSImage alloc] initWithSize:size];

    [targetImage lockFocus];
    [sourceImageRep drawInRect: targetFrame];
    [targetImage unlockFocus];

return targetImage; 
}

它来自我在这里给出的更详细的答案:NSImage doesn't scale

另一个有效的调整大小的方法是NSImage方法drawInRect:fromRect:operation:fraction:respectFlipped:hints

代码语言:javascript
运行
复制
- (void)drawInRect:(NSRect)dstSpacePortionRect 
          fromRect:(NSRect)srcSpacePortionRect 
         operation:(NSCompositingOperation)op 
          fraction:(CGFloat)requestedAlpha 
    respectFlipped:(BOOL)respectContextIsFlipped 
             hints:(NSDictionary *)hints

此方法的主要优点是hints NSDictionary,在其中您可以对插值进行一些控制。这会在放大图像时产生截然不同的结果。NSImageHintInterpolation是一个枚举,可以接受以下五个值之一:…

代码语言:javascript
运行
复制
    enum {
       NSImageInterpolationDefault = 0,
       NSImageInterpolationNone = 1,
       NSImageInterpolationLow = 2,
       NSImageInterpolationMedium = 4,
       NSImageInterpolationHigh = 3
    };
    typedef NSUInteger NSImageInterpolation;

使用这种方法不需要提取imageRep的中间步骤,NSImage会做正确的事情……

代码语言:javascript
运行
复制
- (NSImage*) resizeImage:(NSImage*)sourceImage size:(NSSize)size
{
    NSRect targetFrame = NSMakeRect(0, 0, size.width, size.height);
    NSImage*  targetImage = [[NSImage alloc] initWithSize:size];

    [targetImage lockFocus];

    [sourceImage drawInRect:targetFrame
                   fromRect:NSZeroRect       //portion of source image to draw 
                  operation:NSCompositeCopy  //compositing operation
                   fraction:1.0              //alpha (transparency) value
             respectFlipped:YES              //coordinate system
                      hints:@{NSImageHintInterpolation:
     [NSNumber numberWithInt:NSImageInterpolationLow]}];

    [targetImage unlockFocus];

    return targetImage;
}
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15587527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档