如何拍摄质量低的截图

时间:2015-02-02 08:39:38

标签: objective-c image macos screenshot resolution

有没有办法以编程方式在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大小,我无法读取图像中的任何字符。但是,我希望尽可能降低这些图像。有任何建议或其他可能的解决方案吗?

感谢。

1 个答案:

答案 0 :(得分:0)

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

然后压缩它:)

//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