2012-06-12 170 views
3

此代碼來自Apple的WWDC 2011 Session 318 - 深度iOS性能,並使用CoreGraphics從服務器託管的圖像創建縮略圖。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文檔CGImageSourceCGImageSourceRefCGImageSourceCreateThumbnailAtIndex

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

我怎樣才能得到這個工作?

編輯

這是我收到的編譯器錯誤:

  • 使用未聲明的標識符 'CGImageSourceRef'
  • 使用未聲明的標識符 'kCGImageSourceThumbnailMaxPixelSize'
  • 使用的未申報的標識符'src'
  • 函數的隱式聲明'CGImageSourceCreateThumbn ailAtIndex」是C99無效
  • 的功能隱式聲明 'CGImageSourceRelease' 是C99無效
  • 的功能隱式聲明 'CGImageSourceCreateWithURL' 是C99
+1

的iOS文件實際上是[這裏](http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGImageSource/Reference/reference.html)和在iOS 4.0及更高版本中聲明可用性。嘗試說明問題的確切來源。你甚至會得到一個有效的'src'。附:在apple.developer論壇之外討論WWDC會話並不是一個好主意 - 因爲所有引用的函數都不在NDA之下,所以我想你是o.k. :) –

+0

啊yep沒有看到iOS 4 +進一步下降。編輯錯誤。此外,這是從去年的WWDC,這是好的沒有? – daidai

+0

我想應該沒問題,雖然我記得在某個地方唱disclamer。 –

回答

15

校園男孩的錯誤。

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

+0

這裏同樣的錯誤... – Jasmeet

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

我用它在我的代碼一段時間,但我不記得它的來源

試試這也 resizing a UIImage without loading it entirely into memory?

+0

謝謝你,但我特別想使用'CGImageSourceCreateThumbnailAtIndex'函數 – daidai

+1

http://stackoverflow.com/questions/5860215/resizing-a-uiimage-without-loading-it-entirely-into-memory試試這個請 –