【问题标题】:CoreGraphics Image resizeCoreGraphics 图像调整大小
【发布时间】:2012-06-15 04:05:35
【问题描述】:

此代码来自 Apple 的 WWDC 2011 Session 318 - iOS Performance in Depth,并使用 CoreGraphics 从服务器托管的图像创建缩略图。

CGImageSourceRef src = CGImageSourceCreateWithURL(url);
NSDictionary *options = (CFDictionaryRef)[NSDictionary 
dictionaryWithObject:[NSNumber numberWithInt:1024
forKey:(id)kCGImageSourceThumbnailMaxPixelSize];

CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src,0,options);
UIImage *image = [UIImage imageWithCGImage:thumbnail];
CGImageRelease(thumbnail);
CGImageSourceRelease(src); 

但它不起作用,文档也没有真正的帮助。在 iOS 文档中 CGImageSource CGImageSourceRef CGImageSourceCreateThumbnailAtIndex 可用

在 Mac OS X v10.4 或更高版本中

我怎样才能让它工作?

编辑

这些是我得到的编译器错误:

  • 使用未声明的标识符“CGImageSourceRef”
  • 使用未声明的标识符“kCGImageSourceThumbnailMaxPixelSize”
  • 使用未声明的标识符“src”
  • 函数“CGImageSourceCreateThumbnailAtIndex”的隐式声明在 C99 中无效
  • 函数“CGImageSourceRelease”的隐式声明在 C99 中无效
  • 函数“CGImageSourceCreateWithURL”的隐式声明在 C99 中无效

【问题讨论】:

  • iOS 文档实际上是 here 并且可用性声明为 in iOS 4.0 and later。尝试说明问题的确切根源。你甚至得到一个有效的src。附言在 apple.developer 论坛之外讨论 WWDC 会议不是一个好主意——因为所有引用的函数都不在 NDA 之下,我想你没问题。 :)
  • 啊,是的,没有看到 iOS 4+ 进一步下降。编辑有错误。另外,这是去年 WWDC 的,这很好,不是吗?
  • 我想应该没问题,虽然我记得在某处看到过免责声明。

标签: iphone ios ios5 core-graphics


【解决方案1】:

小学生的错误。

没有添加#import <ImageIO/ImageIO.h>

【讨论】:

    【解决方案2】:

    尝试调整图片大小:

    -(UIImage*) resizedImage:(UIImage *)inImage:(CGRect) thumbRect
    {
        CGImageRef          imageRef = [inImage CGImage];
        CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);
    
        // There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
        // see Supported Pixel Formats in the Quartz 2D Programming Guide
        // Creating a Bitmap Graphics Context section
        // only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
        // and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
        // The images on input here are likely to be png or jpeg files
        if (alphaInfo == kCGImageAlphaNone)
            alphaInfo = kCGImageAlphaNoneSkipLast;
    
        // Build a bitmap context that's the size of the thumbRect
        CGContextRef bitmap = CGBitmapContextCreate(
                                                    NULL,
                                                    thumbRect.size.width,       // width
                                                    thumbRect.size.height,      // height
                                                    CGImageGetBitsPerComponent(imageRef),   // really needs to always be 8
                                                    4 * thumbRect.size.width,   // rowbytes
                                                    CGImageGetColorSpace(imageRef),
                                                    alphaInfo
                                                    );
    
        // Draw into the context, this scales the image
        CGContextDrawImage(bitmap, thumbRect, imageRef);
    
        // Get an image from the context and a UIImage
        CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
        UIImage*    result = [UIImage imageWithCGImage:ref];
    
        CGContextRelease(bitmap);   // ok if NULL
        CGImageRelease(ref);
    
        return result;
    }
    

    我在我的代码中使用了一段时间,但我不记得它的来源

    也试试这个 resizing a UIImage without loading it entirely into memory?

    【讨论】:

    猜你喜欢
    • 2015-10-26
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    相关资源
    最近更新 更多