我正在尝试编写一个捕获屏幕截图的小型库,并将图像存储在磁盘上。
我能够使屏幕截图工作,并将图像存储在磁盘上。现在,我无法根据指定的高度和宽度调整图像大小。以下是代码段:
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");
如果我做错了,请告诉我。
答案 0 :(得分:3)
不确定,如果没有看到你如何制作CGImageRef,可能会有很多事情发生,很难看到问题。 你有没有尝试使用NSImage,这非常容易使用。
NSImage * img = [[NSImage alloc] initWithCGImage:imageRef size:NSZeroSize];
[img setSize: NSMakeSize(imageWidth,imageHeight)];
//do something with img;
[img release];
如果你没有设置断点,并确保你认为有效的一切都是有效的。
答案 1 :(得分:1)
我认为kCGImagePropertyDPIWidth
和kCGImagePropertyDPIHeight
不是CGImageDestinationAddImage
的有意义选项。您需要创建具有给定大小的新图像,并将 写入目标。
这可以通过多种方式完成,但最简单的可能是通过格雷迪建议的NSImage
。您还可以创建所需大小和像素格式的新CGBitmapContext
,使用CGContextDrawImage
将现有图片绘制到其中,然后使用CGImageRef
提取新的CGBitmapContextCreateImage
。这有点乏味,但应该让你到达你想去的地方。
答案 2 :(得分:1)
这是我的工作,不确定这是否有效。
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 );