ios - 如何缩小 UIImage 并同时使其变得清晰/清晰而不是模糊?

标签 ios image uikit core-graphics scaling

我需要缩小图像,但要以锐利的方式。例如,在 Photoshop 中有图像尺寸缩小选项“Bicubic Smoother”(模糊)和“Bicubic Sharper”。

此图像缩小算法是否开源或在某处记录或 SDK 是否提供执行此操作的方法?

最佳答案

仅仅使用 imageWithCGImage 是不够的。它会缩放,但无论是放大还是缩小,结果都将是模糊的和次优的。

如果你想获得正确的别名并摆脱“锯齿”,你需要这样的东西:http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ .

我的工作测试代码看起来像这样,这是 Trevor 的解决方案,只需一个小调整即可使用我的透明 PNG:

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGImageRef imageRef = image.CGImage;

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

    CGContextConcatCTM(context, flipVertical);  
    // Draw into the context; this scales the image
    CGContextDrawImage(context, newRect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    CGImageRelease(newImageRef);
    UIGraphicsEndImageContext();    

    return newImage;
}

关于ios - 如何缩小 UIImage 并同时使其变得清晰/清晰而不是模糊?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6141298/

相关文章:

ios - 使用循环生成 uisegmentedcontrol 按钮

image - 如何在视频的第一帧之前放置静止图像?

比较两个图像的算法

ios - UIStoryboardSegue & UIVisualEffectView - 完成 block 退出后模糊效果丢失

iphone - 将 Cocos2D 与 UIKit 集成

ios - Apple store connect 不显示我的最新版本

ios - ViewController 正在加载一个不存在的 Nib

ios - 在不损害应用程序性能的情况下,从 Core Data 执行级联对象删除的有效方法是什么?

android - 是否有任何特殊网站可以为移动应用程序获取丰富的背景/图像?

iphone - 将图像添加到 UItableView