核心图形图像调整大小

3
这段代码来自苹果的WWDC 2011会议318-深入了解iOS性能,使用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' - 在C99中,隐式声明函数'CGImageSourceCreateThumbnailAtIndex'无效 - 在C99中,隐式声明函数'CGImageSourceRelease'无效 - 在C99中,隐式声明函数'CGImageSourceCreateWithURL'无效

1
iOS文档实际上在这里(http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGImageSource/Reference/reference.html),可用性声明为“iOS 4.0及更高版本”。尝试说明问题的确切来源。您是否获得了有效的“src”?顺便说一句,最好不要在apple.developer论坛之外讨论WWDC会议 - 因为所有引用的函数都不受NDA的约束,我想你没问题 :) - Rok Jarc
啊,是的,我没有看到下面还有iOS 4+。编辑时出现了错误。另外,这是去年的WWDC,这没关系吧? - daihovey
我想应该没问题,尽管我记得在某处看到过免责声明。 - Rok Jarc
2个回答

15

初学者错误。

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


3
尝试调整图片大小:
-(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;
}

我曾经在我的代码中使用它,但是我记不得它的来源了。
也可以尝试这个: 如何调整UIImage的大小而无需完全加载它?

谢谢您的回复,但我特别想使用CGImageSourceCreateThumbnailAtIndex函数。 - daihovey
1
请尝试这个:https://dev59.com/CW025IYBdhLWcg3wr4F6 - Mina Nabil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接