如何截取低质量的屏幕截图

How to take a screenshot with low quality

有没有办法以编程方式在 osx 上截取屏幕截图(低质量)?

我开发了如下功能:

CGImageRef resizeImage(CGImageRef imageRef) {
    CGRect thumRect;
    CGPoint point;
    point.x = 0;
    point.y = 0;
    thumRect.origin = point;
    thumRect.size.height = 225;
    thumRect.size.width = 360;

    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);

    if (aplhaInfo == kCGImageAlphaNone)
        alphaInfo = kCGImageAlphaNoneSkipLast;

   CGContextRef bitmap = CGBitmapContextCreate(NULL, thumRect.size.width, thumRect.size.height, CGImageGetBitsPerComponent(imageRef), 4 * thumRect.size.width, CGImageGetColorSpace(imageRef), alphaInfo);

   CGContextDrawImage(bitmap, thumRect, imageRef);
   imageRef = CGBitmapContextCreateImage(bitmap);
   CGContextRelease(bitmap);

return imageRef;
}

当我运行这个函数时,我拍摄了一张 150KB 到 600KB 的图像。如果我减小 thumRect 的大小,我将无法读取图像中的任何字符。但是,我想尽可能低地减少这些图像。有什么建议或其他可能的解决方案吗?

谢谢。

我找到了如下解决方案: 首先,用我问题中的代码调整你的图片大小。

然后压缩:)

//imageRef is CGImageRef
NSImage * image = [[NSImage alloc] initWithCGImage:imageRef size:NSZeroSize];
NSBitmapImageRep *bmpImageRep = [NSBitmapImageRep imageRepWithData [image TIFFRepresentation]];

CGFloat compressionFactor = 1.0 //Read it : (1)
NSDictionary *jpgProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithDouble:compressionFactor], NSImageCompressionFactor, [NSNumber numberWithBool:NO], NSImageProgressive, nil];

NSData *jpgData = [bmpImageRep representationUsingType:NSJPEGFileType properties:jpgProperties];

(1):https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBitmapImageRep_Class/index.html#//apple_ref/doc/constant_group/Bitmap_image_properties