2011-04-04 74 views
9

在我的應用程序中,我需要調整大小和裁剪一些圖像,存儲在本地和聯機。我正在使用Trevor Harmon's tutorial,它實現了UIImage+ResizeiPhone CGContextRef CGBitmapContextCreate不支持的參數組合

在我的iPhone 4(iOS 4.3.1)上一切正常,我沒有問題。但在我的iPhone 3G(iOS 3.2)上,調整大小和裁剪方法不適用於任何圖片(本地存儲的圖片是PNG)。 這是控制檯輸出:

Tue Apr 5 02:34:44 Andreis-MacBook-Pro.local Puzzle[12453] <Error>: CGBitmapContextCreate:  unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 288 bytes/row. 
Tue Apr 5 02:34:44 Andreis-MacBook-Pro.local Puzzle[12453] <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 288 bytes/row. 
Tue Apr 5 02:34:44 Andreis-MacBook-Pro.local Puzzle[12453] <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 288 bytes/row. 
Tue Apr 5 02:34:44 Andreis-MacBook-Pro.local Puzzle[12453] <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 288 bytes/row. 

這是作物的方法

- (UIImage *)croppedImage:(CGRect)bounds 
{ 
    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds); 
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    return croppedImage; 
} 

容量調整方法是這樣的:

- (UIImage *)resizedImage:(CGSize)newSize 
      transform:(CGAffineTransform)transform 
     drawTransposed:(BOOL)transpose 
interpolationQuality:(CGInterpolationQuality)quality 
{ 
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height)); 
    CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width); 
    CGImageRef imageRef = self.CGImage; 

    CGContextRef bitmap = CGBitmapContextCreate(NULL, 
              newRect.size.width, 
              newRect.size.height, 
              CGImageGetBitsPerComponent(imageRef), 
              0, 
              CGImageGetColorSpace(imageRef), 
              CGImageGetBitmapInfo(imageRef)); 
    if(bitmap == nil) 
     return nil; 

    CGContextConcatCTM(bitmap, transform); 

    CGContextSetInterpolationQuality(bitmap, quality); 

    CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef); 

    CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap); 
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; 

    CGContextRelease(bitmap); 
    CGImageRelease(newImageRef); 

    return newImage; 
} 

有人能向我解釋道我有這個問題?

謝謝 安德烈

回答

3

確定這可能會或可能不會幫助你(我理解這是一個老帖子)

我得到了類似的問題,因爲寬度參數(在你的情況newRect.size.width)有一個小數部分(例如100.001而不是100.0)。我將它轉換爲一個整數並返回,截斷小數部分,問題就消失了。我猜測有一個測試,看看每個組件x像素等的位數加起來,它不能處理分數像素/點。如果有幫助,歡迎使用此方法。

+(CGSize) fixSize:(CGSize) forSize{ 
    NSInteger w = (NSInteger) forSize.width; 
    NSInteger h = (NSInteger) forSize.height; 
    return CGSizeMake(w, h); 
} 
+0

我在iOS 6上觀察到完全相同的錯誤。這個評論可能爲我省下了一段漫長的顛簸顛簸。 – er0 2013-07-06 20:13:57

5

我覺得這是色彩空間的問題。只需將CGImageGetColorSpace(imageRef)替換爲CGColorSpaceCreateDeviceRGB()即可。這在我嘗試保存從AVCaptureSession獲得的圖像時適用於我。不要忘記釋放它!

CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); 
CGContextRef bitmap = CGBitmapContextCreate(NULL, 
              newRect.size.width, 
              newRect.size.height, 
              CGImageGetBitsPerComponent(imageRef), 
              0, 
              rgbColorSpace,//CGImageGetColorSpace(imageRef), sometimes contains unsupported colorspace 
              bitmapInfo); 
CGColorSpaceRelease(rgbColorSpace); 
3

我曾與iOS 5模擬器同樣的問題,上述問題的答案並沒有解決我的問題:沒有加載圖像和控制檯仍報同樣的錯誤。

我使用的是非常流行的類別here

this blog人們有同樣的問題。馬特2011年11月22日的回答幫助了我。

乾杯!

0

我在模擬器中看到這個問題時,從5.1切換到6.1進行測試。關閉模擬器並再次打開它似乎已經消除了錯誤。

20

在這裏回覆,因爲當我得到這個錯誤時我有完全相同的像素格式。我希望這個答案有助於某人。

它之所以不靈了,在我的情況,是kCGImageAlphaLast不是允許值了在iOS 8,但它運作良好,在iOS 7。32 PBB,8 BPC組合只允許kCGImageAlphaNoneSkip*kCGImageAlphaPremultiplied*爲Alpha信息。顯然這總是一個問題,但在iOS 8之前沒有實施。這裏是我的解決方案:

- (CGBitmapInfo)normalizeBitmapInfo:(CGBitmapInfo)oldBitmapInfo { 
    //extract the alpha info by resetting everything else 
    CGImageAlphaInfo alphaInfo = oldBitmapInfo & kCGBitmapAlphaInfoMask; 

    //Since iOS8 it's not allowed anymore to create contexts with unmultiplied Alpha info 
    if (alphaInfo == kCGImageAlphaLast) { 
     alphaInfo = kCGImageAlphaPremultipliedLast; 
    } 
    if (alphaInfo == kCGImageAlphaFirst) { 
     alphaInfo = kCGImageAlphaPremultipliedFirst; 
    } 

    //reset the bits 
    CGBitmapInfo newBitmapInfo = oldBitmapInfo & ~kCGBitmapAlphaInfoMask; 

    //set the bits to the new alphaInfo 
    newBitmapInfo |= alphaInfo; 

    return newBitmapInfo; 
} 

在我的情況的代碼失敗的一塊看起來像這樣,在imageRef是從應用程序捆綁加載的PNG的CGImageRef:

CGContextRef bitmap = CGBitmapContextCreate(NULL, 
               newRect.size.width, 
               newRect.size.height, 
               CGImageGetBitsPerComponent(imageRef), 
               0, 
               CGImageGetColorSpace(imageRef), 
               CGImageGetBitmapInfo(imageRef)); 

來源: https://stackoverflow.com/a/19345325/3099609

https://developer.apple.com/library/mac/DOCUMENTATION/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203-BCIBHHBB

+2

對於任何人使用[Trevor Harmon的教程](http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/)中的UIImage類別,代碼OP正在使用,這是解決這個問題的好方法。 – Bigood 2015-05-18 16:01:09