简体   繁体   English

调整图像大小 - Cocoa - MAC OS X

[英]Resizing image - Cocoa - MAC OS X

I am trying to write a small library that captures the screen shot and stores the image on the disk.我正在尝试编写一个小型库来捕获屏幕截图并将图像存储在磁盘上。

I was able to get the screen shot working and also to get the image stored on a disk.我能够使屏幕截图正常工作,也能够将图像存储在磁盘上。 Now, I am not able to resize the image based on the specified height and width.现在,我无法根据指定的高度和宽度调整图像大小。 Here is the code snippet:这是代码片段:

int imageWidth = 200;
int imageHeight = 200;

CFStringRef keys[2];
CFTypeRef   values[2];
keys[0]   = kCGImagePropertyDPIHeight;
values[0] = CFNumberCreate(NULL, kCFNumberIntType, &imageHeight);
keys[1]   = kCGImagePropertyDPIWidth;
values[1] = CFNumberCreate(NULL, kCFNumberIntType, &imageWidth);


CFDictionaryRef options = NULL;
options = CFDictionaryCreate( NULL, (const void **)keys, (const void **)values, 2,
                               &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

// Set the image in the image destination to be `image' with
// optional properties specified in saved properties dict.
CGImageDestinationAddImage(dest, imageRef, options);

bool success = CGImageDestinationFinalize(dest);
NSAssert( success != 0, @"Image could not be written successfully");

Please let me know if I am doing something wrong.如果我做错了什么,请告诉我。

not sure, there could be a lot of things going on without seeing how you are making the CGImageRef, it would be tough to see a problem.不确定,可能会发生很多事情而没有看到你是如何制作 CGImageRef 的,很难看到问题。 have you tried to use an NSImage, which is super easy to work with.您是否尝试过使用非常容易使用的 NSImage。

NSImage * img = [[NSImage alloc] initWithCGImage:imageRef size:NSZeroSize];
[img setSize: NSMakeSize(imageWidth,imageHeight)];

//do something with img;
[img release];

Failing that you could set some breakpoints, and make sure everything that you think is valid, really is.如果失败,您可以设置一些断点,并确保您认为有效的所有内容都是有效的。

I don't think kCGImagePropertyDPIWidth and kCGImagePropertyDPIHeight are meaningful options for CGImageDestinationAddImage .我不认为kCGImagePropertyDPIWidthkCGImagePropertyDPIHeightCGImageDestinationAddImage有意义的选项。 You need to create a new image with the given size and write that to the destination.您需要创建一个具有给定大小的新图像并将写入目标。

This can be done in several ways, but the simplest is probably going via NSImage as Grady suggests.这可以通过多种方式完成,但最简单的可能是通过 Grady 建议的NSImage You could also create a new CGBitmapContext of the desired size and pixel format, draw the existing image into it with CGContextDrawImage , then extract a new CGImageRef with CGBitmapContextCreateImage .您还可以创建所需大小和像素格式的新CGBitmapContext ,使用CGContextDrawImage将现有图像绘制到其中,然后使用CGBitmapContextCreateImage CGImageRef It's a bit tedious, but should get you where you want to go.这有点乏味,但应该能让你到达你想要的 go。

This is doing my work, not sure whether this is efficient.这是在做我的工作,不确定这是否有效。

int imageWidth  = 200;
int imageHeight = 200;

CGContextRef    context = NULL;
CGColorSpaceRef colorSpace;
void *          bitmapData;
int             bitmapByteCount;
int             bitmapBytesPerRow;

// Get image width, height. We'll use the entire image.
size_t pixelsWide = imageWidth;//CGImageGetWidth(imageRef);
size_t pixelsHigh = imageHeight;//CGImageGetHeight(imageRef);

// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow   = (pixelsWide * 4);
bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

// Use the generic RGB color space.
colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);

// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
bitmapData = malloc( bitmapByteCount );

// Create the bitmap context. We want pre-multiplied ARGB, 8-bits 
// per component. Regardless of what the source image format is 
// (CMYK, Grayscale, and so on) it will be converted over to the format
// specified here by CGBitmapContextCreate.
context = CGBitmapContextCreate (bitmapData,
                                 pixelsWide,
                                 pixelsHigh,
                                 8,      // bits per component
                                 bitmapBytesPerRow,
                                 colorSpace,
                                 kCGImageAlphaPremultipliedFirst);

// Make sure and release colorspace before returning
CGColorSpaceRelease( colorSpace );

// Get image width, height. We'll use the entire image.
size_t w = CGImageGetWidth(imageRef);
size_t h = CGImageGetHeight(imageRef);
CGRect rect = {{0,0},{imageWidth,imageHeight}}; 

// Draw the image to the bitmap context. Once we draw, the memory 
// allocated for the context for rendering will then contain the 
// raw image data in the specified color space.
CGContextDrawImage(context, rect, imageRef);

CGImageRef outImageRef = NULL;

outImageRef = CGBitmapContextCreateImage( context );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM