标签: nsbitmapimagerep

如何从NSImage保存PNG文件(视网膜问题)

我正在对图像进行一些操作,在完成之后,我想将图像保存为磁盘上的PNG.我正在做以下事情:

+ (void)saveImage:(NSImage *)image atPath:(NSString *)path {

    [image lockFocus] ;
    NSBitmapImageRep *imageRepresentation = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0.0, 0.0, image.size.width, image.size.height)] ;
    [image unlockFocus] ;

    NSData *data = [imageRepresentation representationUsingType:NSPNGFileType properties:nil];
    [data writeToFile:path atomically:YES];
}
Run Code Online (Sandbox Code Playgroud)

这段代码工作正常,但问题出在视网膜mac上,如果我打印NSBitmapImageRep对象,我得到一个不同的大小和像素rect,当我的图像保存在磁盘上时,它的大小是两倍:

$0 = 0x0000000100413890 NSBitmapImageRep 0x100413890 Size={300, 300} ColorSpace=sRGB IEC61966-2.1 colorspace BPS=8 BPP=32 Pixels=600x600 Alpha=YES Planar=NO Format=0 CurrentBacking=<CGImageRef: 0x100414830>
Run Code Online (Sandbox Code Playgroud)

因为我想要保留原始大小,我强制像素大小不要关心视网膜比例:

imageRepresentation.pixelsWide = image.size.width;
imageRepresentation.pixelsHigh = image.size.height;
Run Code Online (Sandbox Code Playgroud)

这次我在打印NSBitmapImageRep对象时得到了正确的大小,但是当我保存文件时,我仍然遇到同样的问题:

$0 = 0x0000000100413890 NSBitmapImageRep 0x100413890 Size={300, 300} ColorSpace=sRGB IEC61966-2.1 colorspace BPS=8 BPP=32 Pixels=300x300 Alpha=YES Planar=NO Format=0 CurrentBacking=<CGImageRef: 0x100414830>
Run Code Online (Sandbox Code Playgroud)

知道如何解决这个问题,并保留原始像素大小?

macos nsimage image-resizing nsbitmapimagerep retina-display

26
推荐指数
4
解决办法
2万
查看次数

NSImage到NSBitmapImageRep

如何将NSImage转换为NSBitmapImageRep?我有代码:

- (NSBitmapImageRep *)bitmapImageRepresentation
{
    NSBitmapImageRep *ret = (NSBitmapImageRep *)[self representations];

    if(![ret isKindOfClass:[NSBitmapImageRep class]])
    {
        ret = nil;
        for(NSBitmapImageRep *rep in [self representations])
            if([rep isKindOfClass:[NSBitmapImageRep class]])
            {
                ret = rep;
                break;
            }
    }

    if(ret == nil)
    {
        NSSize size = [self size];

        size_t width         = size.width;
        size_t height        = size.height;
        size_t bitsPerComp   = 32;
        size_t bytesPerPixel = (bitsPerComp / CHAR_BIT) * 4;
        size_t bytesPerRow   = bytesPerPixel * width;
        size_t totalBytes    = height * bytesPerRow;

        NSMutableData *data = [NSMutableData dataWithBytesNoCopy:calloc(totalBytes, 1) …
Run Code Online (Sandbox Code Playgroud)

macos bitmap objective-c nsimage nsbitmapimagerep

14
推荐指数
2
解决办法
9762
查看次数

如何创建NSImage的8位,4位和1位表示

我用以下代码创建了32位NSImage.

 NSBitmapImageRep *sourceRep = [[NSBitmapImageRep alloc] initWithData: imageData];

        // create a new bitmap representation scaled down

            NSBitmapImageRep *newRep = 
                [[NSBitmapImageRep alloc] 
                    initWithBitmapDataPlanes: NULL
                    pixelsWide: imageSize
                    pixelsHigh: imageSize
                    bitsPerSample: 8
                    samplesPerPixel: 4
                    hasAlpha: YES
                    isPlanar: NO
                    colorSpaceName: NSCalibratedRGBColorSpace
                    bytesPerRow: 0
                    bitsPerPixel: 0];

            // save the graphics context, create a bitmap context and set it as current
            [NSGraphicsContext saveGraphicsState] ;
            NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep: newRep];
            [NSGraphicsContext setCurrentContext: context] ;

            // draw the bitmap image representation in it and restore the context …
Run Code Online (Sandbox Code Playgroud)

macos cocoa nsimage nsbitmapimagerep

10
推荐指数
1
解决办法
1395
查看次数

使用带有geometryFlipped的CALayer的renderInContext:方法

我有一个CALayer(containerLayer),我想在将NSBitmapImageRep数据保存为平面文件之前将其转换为a .containerLayer将其geometryFlipped属性设置为YES,这似乎导致了问题.最终生成的PNG文件正确呈现内容,但似乎不考虑翻转的几何体.我显然正在寻找test.png来准确地表示左边显示的内容.

下面是问题的截图和我正在使用的代码.

一个直观的例子

- (NSBitmapImageRep *)exportToImageRep
{
    CGContextRef context = NULL;
    CGColorSpaceRef colorSpace;
    int bitmapByteCount;
    int bitmapBytesPerRow;

    int pixelsHigh = (int)[[self containerLayer] bounds].size.height;
    int pixelsWide = (int)[[self containerLayer] bounds].size.width;

    bitmapBytesPerRow = (pixelsWide * 4);
    bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);

    colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    context = CGBitmapContextCreate (NULL,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedLast);
    if (context == NULL)
    {
        NSLog(@"Failed to create context.");
        return nil;
    }

    CGColorSpaceRelease(colorSpace);
    [[[self containerLayer] presentationLayer] renderInContext:context];    

    CGImageRef img = …
Run Code Online (Sandbox Code Playgroud)

cocoa core-graphics calayer nsbitmapimagerep

7
推荐指数
1
解决办法
2511
查看次数

如何从NSBitmapImageRep创建具有透明度的8位PNG?

我有一个32位NSBitmapImageRep,其alpha通道基本上是1位值(像素是打开或关闭).

我想将此位图保存为具有透明度的8位PNG文件.如果我使用-representationUsingType:properties:方法NSBitmapImageRep并传入NSPNGFileType,则会创建一个32位的PNG,这不是我想要的.

我知道可以读取8位PNG,它们在预览中打开没有问题,但是是否可以使用任何内置的Mac OS X API编写这种类型的PNG文件?如果有必要,我很高兴下载到Core Image甚至QuickTime.对CGImage文档的粗略检查没有发现任何明显的东西.

编辑: 我已经开始在这个问题上获得赏金,如果有人可以提供32位的工作源代码NSBitmapImageRep并写入具有1位透明度的256色PNG,那就是你的.

macos cocoa png image nsbitmapimagerep

6
推荐指数
1
解决办法
2384
查看次数

cocoa:读取NSImage的像素颜色

我有一个NSImage.我想NSColor在某些x和y 读取像素.Xcode似乎有一个colorAtX:y:方法NSImage,但这导致崩溃说NSImage没有这样的方法.我已经看到了一些示例,您可以在其中创建NSBitmapImageRep并调用相同的方法,但我无法将NSImage成功转换为NSBitmapImageRep.由于NSBitmapImageRep某种原因,它上的像素是不同的.

必须有一个简单的方法来做到这一点.它不可能是这么复杂.

cocoa nsimage nsbitmapimagerep nscolor

5
推荐指数
1
解决办法
4918
查看次数

为什么bitmapImageRepForCachingDisplayInRect:创建一个空图像?

我有一个非常简单的代码,可以捕获视图的位图.这曾经在Leopard中运行,但在Snow Leopard中看起来非常糟糕.

这是代码,响应窗口上的按钮按下:

- (IBAction)snapshot:(id)sender
{
    NSView* view = [[sender window] contentView];
    NSBitmapImageRep* bitmap
        = [view bitmapImageRepForCachingDisplayInRect:[view bounds]];
    NSData *tiff = [bitmap TIFFRepresentation];
    [tiff writeToFile:[@"~/Desktop/snapshot.tiff" stringByExpandingTildeInPath]
           atomically:YES];
}
Run Code Online (Sandbox Code Playgroud)

单击按钮拍摄快照只会产生完全透明的图像.

我在这里完全无能为力,还是这个位图缓存方法被打破了?

一个简单的项目 - 基本上是一个启动NSDocument项目,带有一个调用此代码的按钮 - 可以在这里找到.

cocoa nsview nsbitmapimagerep

4
推荐指数
1
解决办法
1682
查看次数

扩展NSImage并保存

我想扩大64px的图像使其成为512px(即使它是模糊的或像素化的)

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

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];
Run Code Online (Sandbox Code Playgroud)

我尝试过setSize:但它仍然保存64px.

提前致谢!

macos objective-c nsimage nsimageview nsbitmapimagerep

2
推荐指数
1
解决办法
4897
查看次数

无法在Swift中创建NSBitmapImageRep

我有一个用xcode 6.1创建的osx xcode项目我想用它来训练使用SWIFT一点点.

在我的一个视图中,我尝试创建一个NSBitMapImageRep,如下所示:

class BitmapView : NSView {
var image: NSBitmapImageRep!

override func awakeFromNib() {
    var blub = NSBitmapImageRep(bitmapDataPlanes: nil,
        pixelsWide: Int(self.frame.size.width),
        pixelsHigh: Int(self.frame.size.height),
        bitsPerSample: 8,
        samplesPerPixel: 1,
        hasAlpha: false,
        isPlanar: false,
        colorSpaceName: NSCalibratedRGBColorSpace,
        bytesPerRow: 0, bitsPerPixel: 0)!

    //test()
}}
Run Code Online (Sandbox Code Playgroud)

但每次我尝试运行它时,我都会收到以下错误:

Inconsistent set of values to create NSBitmapImageRep fatal error: unexpectedly found nil while unwrapping an Optional value
Run Code Online (Sandbox Code Playgroud)

我猜这是因为bitmapDataPlanes为零.但它是一个可选值,根据文档允许为NULL.传递NSNull()而不是编译.

任何人都可以告诉我我必须通过什么?O_O

macos xcode nsbitmapimagerep swift

2
推荐指数
1
解决办法
2270
查看次数