NSImage不缩放

扫码查看
本文介绍了NSImage不缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个快速应用程序,其中我有一个方法,应该重新缩放一个@ 2x图像到一个常规的。问题是它不会:(

I'm developing a quick app in which I have a method that should rescale a @2x image to a regular one. The problem is that it doesn't :(

为什么?

-(BOOL)createNormalImage:(NSString*)inputRetinaImagePath {

    NSImage *inputRetinaImage = [[NSImage alloc] initWithContentsOfFile:inputRetinaImagePath];



    NSSize size = NSZeroSize;
    size.width = inputRetinaImage.size.width*0.5;
    size.height = inputRetinaImage.size.height*0.5;

    [inputRetinaImage setSize:size];


    NSLog(@"%f",inputRetinaImage.size.height);


    NSBitmapImageRep *imgRep = [[inputRetinaImage representations] objectAtIndex: 0];

    NSData *data = [imgRep representationUsingType: NSPNGFileType properties: nil];

    NSString *outputFilePath = [[inputRetinaImagePath substringToIndex:inputRetinaImagePath.length - 7] stringByAppendingString:@".png"];

    NSLog([@"Normal version file path: " stringByAppendingString:outputFilePath]);
    [data writeToFile:outputFilePath atomically: NO];
    return true;
}


推荐答案

的NSImage的size属性。它不一定指向bitmapRepresentation的像素尺寸,它可以指代例如显示的大小。 NSImage可能有多个bitmapRepresentations用于不同的输出大小。

You have to be very wary of the size attribute of an NSImage. It doesn't necessarily refer to the bitmapRepresentation's pixel dimensions, it could refer to the displayed size for example. An NSImage may have a number of bitmapRepresentations for use at different output sizes.

同样,更改NSImage的size属性不会改变bitmapRepresentations

Likewise, changing the size attribute of an NSImage does nothing to alter the bitmapRepresentations

所以你需要做的是计算你想要的输出图像的大小,然后使用bitmapRepresentation从源NSImage绘制一个新的图像。

So what you need to do is work out the size you want your output image to be, and then draw a new image at that size using a bitmapRepresentation from the source NSImage.

获取该大小取决于您如何获取输入图像和您对它的了解。例如,如果您确信您的输入图像只有一个bitmapImageRep,您可以使用这种类型的事物(作为NSImage上的类别)

Getting that size depends on how you have obtained your input image and what you know about it. For example, if you are confident that your input image has only one bitmapImageRep you can use this type of thing (as a category on NSImage)

  - (NSSize) pixelSize
{
    NSBitmapImageRep* bitmap = [[self representations] objectAtIndex:0];
    return NSMakeSize(bitmap.pixelsWide,bitmap.pixelsHigh);
}

即使你有多个bitmapImageReps,第一个应该是最大的一个,如果这是你的视网膜图像创建的大小,它应该是你以后的视网膜尺寸。

Even if you have a number of bitmapImageReps, the first one should be the largest one, and if that is the size that your Retina image was created at, it should be the Retina size you are after.

当你计算出你的最终尺寸,您可以创建图像:

When you have worked out your final size, you can make the image:

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

}

/ strong>

update

这里有一个更复杂的版本的像素大小获得类别NSImage ...让我们假设没有关于图像,它有多少imageReps,是否具有 bitmapImageReps ...这将返回它可以找到的最大像素尺寸。如果它找不到bitMapImageRep像素尺寸,它将使用任何其他可以获得,这将很可能是边界框尺寸(由eps和pdf使用)。

Here is a more elaborate version of a pixel-size-getting category on NSImage... let's assume nothing about the image, how many imageReps it has, whether it has any bitmapImageReps... this will return the largest pixel dimensions it can find. If it can't find bitMapImageRep pixel dimensions it will use whatever else it can get, which will most likely be bounding box dimensions (used by eps and pdfs).

NSImage + PixelSize.h

NSImage+PixelSize.h

#import <Cocoa/Cocoa.h>
#import <QuartzCore/QuartzCore.h>

@interface NSImage (PixelSize)

- (NSInteger) pixelsWide;
- (NSInteger) pixelsHigh;
- (NSSize) pixelSize;

@end

NSImage + PixelSize.m

NSImage+PixelSize.m

#import "NSImage+PixelSize.h"

@implementation NSImage (Extensions)

- (NSInteger) pixelsWide
{
    /*
     returns the pixel width of NSImage.
     Selects the largest bitmapRep by preference
     If there is no bitmapRep returns largest size reported by any imageRep.
     */
    NSInteger result = 0;
    NSInteger bitmapResult = 0;

    for (NSImageRep* imageRep in [self representations]) {
        if ([imageRep isKindOfClass:[NSBitmapImageRep class]]) {
            if (imageRep.pixelsWide > bitmapResult)
                bitmapResult = imageRep.pixelsWide;
        } else {
            if (imageRep.pixelsWide > result)
                result = imageRep.pixelsWide;
        }
    }
    if (bitmapResult) result = bitmapResult;
    return result;

}

- (NSInteger) pixelsHigh
{
    /*
     returns the pixel height of NSImage.
     Selects the largest bitmapRep by preference
     If there is no bitmapRep returns largest size reported by any imageRep.
     */
    NSInteger result = 0;
    NSInteger bitmapResult = 0;

    for (NSImageRep* imageRep in [self representations]) {
        if ([imageRep isKindOfClass:[NSBitmapImageRep class]]) {
            if (imageRep.pixelsHigh > bitmapResult)
                bitmapResult = imageRep.pixelsHigh;
        } else {
            if (imageRep.pixelsHigh > result)
                result = imageRep.pixelsHigh;
        }
    }
    if (bitmapResult) result = bitmapResult;
    return result;
}

- (NSSize) pixelSize
{
    return NSMakeSize(self.pixelsWide,self.pixelsHigh);
}

@end

c> #importNSImage + PixelSize.h在您当前的文件中,以使其可访问。

You would #import "NSImage+PixelSize.h" in your current file to make it accessible.

使用此图像类别和调整大小:方法,您将修改您的方法,因此:

With this image category and the resize: method, you would modify your method thus:

//size.width = inputRetinaImage.size.width*0.5;
//size.height = inputRetinaImage.size.height*0.5;
size.width  = inputRetinaImage.pixelsWide*0.5;
size.height = inputRetinaImage.pixelsHigh*0.5;

//[inputRetinaImage setSize:size];
NSImage* outputImage = [self resizeImage:inputRetinaImage size:size];

//NSBitmapImageRep *imgRep = [[inputRetinaImage representations] objectAtIndex: 0];
NSBitmapImageRep *imgRep = [[outputImage representations] objectAtIndex: 0];

它在你的代码上)

这篇关于NSImage不缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 19:36
查看更多