Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 调整核心图形中的图像大小_Iphone_Image_Resize_Core Graphics - Fatal编程技术网

Iphone 调整核心图形中的图像大小

Iphone 调整核心图形中的图像大小,iphone,image,resize,core-graphics,Iphone,Image,Resize,Core Graphics,我正在尝试调整CGImageRef的大小,以便可以在屏幕上以我想要的大小绘制它。 所以我有这个代码: CGColorSpaceRef colorspace = CGImageGetColorSpace(originalImage); CGContextRef context = CGBitmapContextCreate(NULL, CGImageGetWidth(originalImage),

我正在尝试调整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)

它实际上返回图像的原始宽度,而不是我在屏幕上看到的宽度

那么,我如何才能真正创建一个实际调整大小的图像,而不仅仅是按照我想要的大小绘制的图像呢


谢谢

问题是您正在创建与图像大小相同的上下文。您希望将上下文设置为新大小。然后剪辑是不必要的

试试这个:

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);

我没有实际测试它,但它至少应该让您知道需要更改哪些内容。

您是只想将其重新调整大小还是真的想调整大小?您可以不做第二个就做第一个(而且可能更快),因为我们正在讨论这个主题,我将为我的UIImage类别制作一个插件。您可以使用-imageWithSize:contentMode:。