如何缩放NSImage, 减少图片的大小?

可以利用drawInRect函数来实现,下面列举两个例子进行介绍:

一、利用 NSImageRep 的 drawInRect 函数,代码如下:

    NSImage* sourceImage = ...;

    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];

 

二、利用 NSImage( NSImageRep 也一样)的稍微复杂一点的函数,可以设置的更详细一点:

代码如下:

    NSImage* sourceImage = ...;

    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];

 

   这两段代码本人都亲自测试过,速度还挺快。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将`CATextLayer`保存为`NSImage`,可以使用以下步骤: 1. 创建一个`CALayer`对象,将`CATextLayer`添加到该层中。 2. 创建一个`NSBitmapImageRep`对象,设置其大小与`CALayer`对象相同。 3. 将`CALayer`对象渲染到`NSBitmapImageRep`对象中。 4. 创建一个`NSImage`对象,并将`NSBitmapImageRep`对象添加到其中。 以下是示例代码: ```objective-c // 创建一个 CATextLayer CATextLayer *textLayer = [CATextLayer layer]; textLayer.string = @"Hello, World!"; textLayer.fontSize = 20; textLayer.frame = CGRectMake(0, 0, 100, 50); // 创建一个 CALayer 并将 textLayer 添加到其中 CALayer *layer = [CALayer layer]; [layer addSublayer:textLayer]; // 创建一个 NSBitmapImageRep 对象 NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:layer.bounds.size.width pixelsHigh:layer.bounds.size.height bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:0 bitsPerPixel:0]; // 渲染 CALayer 对象到 NSBitmapImageRep 对象中 NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:bitmapRep]; [NSGraphicsContext saveGraphicsState]; [NSGraphicsContext setCurrentContext:context]; [layer renderInContext:context.CGContext]; [NSGraphicsContext restoreGraphicsState]; // 创建一个 NSImage 对象并将 NSBitmapImageRep 对象添加到其中 NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(layer.bounds.size.width, layer.bounds.size.height)]; [image addRepresentation:bitmapRep]; ``` 这样,`textLayer`就被保存为了`NSImage`对象`image`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值