缩放黑白图像

发布于 2024-12-12 10:21:41 字数 945 浏览 0 评论 0原文

我从网络上获取图像并将其显示在 UIImage 中,但首先我将其缩小。问题是,如果图像是黑白的,结果会很糟糕,有很多白线穿过图片。

我尝试了很多变化,但没有看到任何改进,总是同样的质量差的结果。有什么建议吗?

我尝试过的一些事情(记住我在没有 UIImageView 的情况下工作):

http://iosdevelopertips.com/graphics/how-to-scale-an-image-using-an-objective-c-category.html

如何按比例缩放 UIImageView?

什么是最简单的方法使用 iPhone SDK 调整/优化图像大小?

http://www.iphonedevsdk.com/forum/iphone-sdk-development/5204-resize-image-high-quality.html

I fetch an image from the web and show it in an UIImage, but first i scaled it down. The thing is, if the image is black and white, the result is pretty bad, with lots of white lines crossing the pic.

I've tried many variations but i see no improvement, always the same poor quality result. Any tips?

Some of the things i tried (remember i'm working without an UIImageView):

http://iosdevelopertips.com/graphics/how-to-scale-an-image-using-an-objective-c-category.html

How to scale a UIImageView proportionally?

What's the easiest way to resize/optimize an image size with the iPhone SDK?

http://www.iphonedevsdk.com/forum/iphone-sdk-development/5204-resize-image-high-quality.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他不在意 2024-12-19 10:21:41

事实证明@cbranch是对的,所以首先这是我找到的解决方案(没有版本):

- (BOOL) isGrayscaleImage:(UIImage *)image
{
    CGImageRef imgRef = [image CGImage];
    CGColorSpaceModel clrMod = CGColorSpaceGetModel(CGImageGetColorSpace(imgRef));

    switch (clrMod) {
        case kCGColorSpaceModelMonochrome : 
            return YES;
        default:
            return NO;
    }
}

- (UIImage *) imageToGrayscale:(UIImage *)image
{
    CGSize size = image.size; 
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height); 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 
    CGContextRef context = CGBitmapContextCreate(nil, size.width, size.height, 8, 0, colorSpace, kCGImageAlphaNone); 
    CGColorSpaceRelease(colorSpace); 
    CGContextDrawImage(context, rect, [image CGImage]); 
    CGImageRef grayscale = CGBitmapContextCreateImage(context);
    UIImage *img = [UIImage imageWithCGImage:grayscale];
    return img;
}

注意,这对于iOS5来说不是必需的,不知道为什么(文档中没有任何关于这一点的内容)。

Turns out @cbranch was right, so first here is the solution i found (without releases):

- (BOOL) isGrayscaleImage:(UIImage *)image
{
    CGImageRef imgRef = [image CGImage];
    CGColorSpaceModel clrMod = CGColorSpaceGetModel(CGImageGetColorSpace(imgRef));

    switch (clrMod) {
        case kCGColorSpaceModelMonochrome : 
            return YES;
        default:
            return NO;
    }
}

- (UIImage *) imageToGrayscale:(UIImage *)image
{
    CGSize size = image.size; 
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height); 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 
    CGContextRef context = CGBitmapContextCreate(nil, size.width, size.height, 8, 0, colorSpace, kCGImageAlphaNone); 
    CGColorSpaceRelease(colorSpace); 
    CGContextDrawImage(context, rect, [image CGImage]); 
    CGImageRef grayscale = CGBitmapContextCreateImage(context);
    UIImage *img = [UIImage imageWithCGImage:grayscale];
    return img;
}

Notice that this isn't necessary for iOS5, don't know why (nothing about this in the documentation).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文