2014-07-21 28 views
0

我收到了一些有關錯誤的UIImage + Resize類別擴展名爲here的UIImage類使用類別擴展名。我正在使用Xcode 5.1.1。UIImage + Resize類別擴展錯誤和警告iOS 7

問題:是否有使用其他的東西Quartz2D或一些其他的框架來解決這些問題,面向未來的UIImage的類別擴展的更好的辦法?

這裏是控制檯輸出:

  • CGBitmapContextCreate不受支持的參數 組合:8個整數位/分量; 32位/像素; 3色成分 色彩空間;無法識別的;每行1200字節。
  • CGContextConcatCTM:invalid context 0x0。這是一個嚴重錯誤 。此應用程序或其使用的庫正在使用無效的上下文,從而導致整個系統穩定性和可靠性下降。此通知是 禮貌:請解決此問題。這將成爲 即將到來的更新中的致命錯誤。
  • CGContextSetInterpolationQuality:invalid context 0x0。這是一個嚴重的錯誤。此應用程序或其使用的庫, 正在使用無效的上下文,從而導致整個系統穩定性和可靠性下降。這個 通知是一個禮貌:請解決這個問題。在即將到來的更新中,它將成爲 致命錯誤。
  • CGContextDrawImage:無效的上下文0x0。這是一個嚴重的錯誤。此應用程序或其使用的庫正在使用無效的上下文,因此有助於穩定性和可靠性的整體降級。此通知是禮貌性的:請修復這個問題 。這將成爲即將到來的更新中的致命錯誤。
  • CGBitmapContextCreateImage:invalid context 0x0。這 是一個嚴重的錯誤。此應用程序或其使用的庫正在使用無效的上下文,從而導致整體系統穩定性和可靠性下降。此通知是
    禮貌:請解決此問題。在即將到來的更新中,這將成爲致命錯誤 。

我抓住了ImageView的圖像,並調用方法:

if (image != nil) { 
      //present our image in the image view 
      _imageView.image = image; 

      //make half size thumbnail. 
      CGSize imageSize = CGSizeMake(300,300); 

      image = [image resizedImage:imageSize interpolationQuality:kCGInterpolationHigh]; 
} 

這裏的範疇擴展專用輔助方法包含了所有的問題:

// Returns a copy of the image that has been transformed using the given affine transform and scaled to the new size 
// The new image's orientation will be UIImageOrientationUp, regardless of the current image's orientation 
// If the new size is not integral, it will be rounded up 
- (UIImage *)resizedImage:(CGSize)newSize 
       transform:(CGAffineTransform)transform 
      drawTransposed:(BOOL)transpose 
    interpolationQuality:(CGInterpolationQuality)quality { 
    CGFloat scale = MAX(1.0f, self.scale); 
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width*scale, newSize.height*scale)); 
    CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width); 
    CGImageRef imageRef = self.CGImage; 

    // Fix for a colorspace/transparency issue that affects some types of 
    // images. See here: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/comment-page-2/#comment-39951 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef bitmap = CGBitmapContextCreate(
               NULL, 
               newRect.size.width, 
               newRect.size.height, 
               8, /* bits per channel */ 
               (newRect.size.width * 4), /* 4 channels per pixel * numPixels/row */ 
               colorSpace, 
               kCGBitmapByteOrderDefault 
               ); 
    CGColorSpaceRelease(colorSpace); 

    // Rotate and/or flip the image if required by its orientation 
    CGContextConcatCTM(bitmap, transform); 

    // Set the quality level to use when rescaling 
    CGContextSetInterpolationQuality(bitmap, quality); 

    // Draw into the context; this scales the image 
    CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef); 

    // Get the resized image from the context and a UIImage 
    CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap); 
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:self.scale orientation:UIImageOrientationUp]; 

    // Clean up 
    CGContextRelease(bitmap); 
    CGImageRelease(newImageRef); 

    return newImage; 
} 
+0

我在IOS8中獲得相同的警告,此外,在IOS8中返回的uiimage是零,所以我看不到調整大小的圖像...你能解決嗎?在我的情況下爲 –

+1

mmmm。我發現無法創建上下文:CGBitmapContextCreate:不支持的參數組合:8個整數位/組件; 32位/像素; 3分量色彩空間; kCGImageAlphaLast; 448字節/行。 :( –

+0

嘿@EvaMadrazo看看我的回答下面,我希望這可以幫助你!和平。 –

回答

1

我能得到問題解決了經過一番處理後,用於Quartz的CGBitmapContextCreate

有一些「分支」可以解釋爲什麼這個工程,但從iOS7和現在最近,iOS8(Xcode 6.0),這個代碼適用於我。下面是一段代碼,我改變:

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef bitmap = CGBitmapContextCreate(
               NULL, 
               newRect.size.width, 
               newRect.size.height, 
               CGImageGetBitsPerComponent(imageRef), /* bits per channel */ 
               (newRect.size.width * 4), /* 4 channels per pixel * numPixels/row */ 
               colorSpace, 
               CGImageGetBitmapInfo(imageRef)/*SOLVED: instead of a constant here add this CGImageGetBitmapInfo(imageRef)*/ 
               ); 
    CGColorSpaceRelease(colorSpace); 

我不得不改變像它被硬編碼(的size_t bitsPerComponent)我用CGImageGetBitsPerComponentimageRef)部分出頭,因爲我想它是不管imageRef輸入的位數不僅僅是8位。下一個位(雙關語意)是解決暴躁「kCGBitmapByteOrderDefault」,這是吹塊,但是在這個時候我發現是有用的,所以我用CGImageGetBitmapInfoimageRef),其爲動態是它返回一個常數(返回位圖圖像的位圖信息),其指定位圖數據的類型以及阿爾法通道是否在數據中。

我希望這有助於某人,我歡迎任何人幫助解釋這一點,因爲我知道有更好的人在這裏做。乾杯!

+0

首先,感謝您的回答。這很奇怪,但您的代碼是我首先測試的原始代碼,它給了我我得到的錯誤是::CGBitmapContextCreate:不支持的參數組合:8個整數位/分量; 32位/像素; 3分量色彩空間; kCGImageAlphaLast ; 436字節/行 –

+2

在我的情況。改變CGImageGetBitmapInfo(imageRef) - > kCGImageAlphaPremultipliedFirst使魔術(魔術,因爲我不明白爲什麼!:)) –

+0

@EvaMadrazo我很高興你找到了一個「神奇」的解決方案!大聲笑 –