我正在尝试调整CGImageRef的大小,以便可以按照我想要的大小在屏幕上绘制它。 因此,我有以下代码:
CGColorSpaceRef colorspace = CGImageGetColorSpace(originalImage);
CGContextRef context = CGBitmapContextCreate(NULL,
CGImageGetWidth(originalImage),
CGImageGetHeight(originalImage),
CGImageGetBitsPerComponent(originalImage),
CGImageGetBytesPerRow(originalImage),
colorspace,
CGImageGetAlphaInfo(originalImage));
if(context == NULL)
return nil;
CGRect clippedRect = CGRectMake(CGContextGetClipBoundingBox(context).origin.x,
CGContextGetClipBoundingBox(context).origin.y,
toWidth,
toHeight);
CGContextClipToRect(context, clippedRect);
// draw image to context
CGContextDrawImage(context, clippedRect, originalImage);
// extract resulting image from context
CGImageRef imgRef = CGBitmapContextCreateImage(context);
这段代码似乎允许我按照所需大小绘制图像,这很好。
问题在于,即使屏幕上看起来已经被缩放,但实际上我获得的图像并没有被缩放。当我执行:
CGImageGetWidth(imgRef);
它实际上返回的是原始图像的宽度,而不是我在屏幕上看到的宽度。
那么,如何才能创建一个真正被缩放而不仅仅是按照所需大小绘制的图像呢?
谢谢