zum*_*zum 5 iphone resize image core-graphics
我正在尝试调整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);
Run Code Online (Sandbox Code Playgroud)
所以这段代码允许我显然将图像绘制到我想要的大小,这很好.问题是我得到的实际图像一旦调整大小,即使它看起来在屏幕上调整大小它实际上没有调整大小.当我做:
CGImageGetWidth(imgRef);
它实际上返回了图像的原始宽度,而不是我在屏幕上看到的宽度.
那么我怎么能真正创建一个实际调整大小的图像,而不仅仅是我想要的正确尺寸?
谢谢
问题是您正在创建与图像大小相同的上下文。您想让上下文成为新的大小。然后就不需要剪辑了。
尝试这个:
CGColorSpaceRef colorspace = CGImageGetColorSpace(originalImage);
CGContextRef context = CGBitmapContextCreate(NULL,
toWidth, // Changed this
toHeight, // Changed this
CGImageGetBitsPerComponent(originalImage),
CGImageGetBytesPerRow(originalImage)/CGImageGetWidth(originalImage)*toWidth, // Changed this
colorspace,
CGImageGetAlphaInfo(originalImage));
if(context == NULL)
return nil;
// Removed clipping code
// draw image to context
CGContextDrawImage(context, CGContextGetClipBoundingBox(context), originalImage);
// extract resulting image from context
CGImageRef imgRef = CGBitmapContextCreateImage(context);
Run Code Online (Sandbox Code Playgroud)
我实际上没有测试它,但它至少应该让您了解需要更改的内容。
| 归档时间: |
|
| 查看次数: |
3549 次 |
| 最近记录: |