2010-06-02 25 views
2

錯誤:CGBitmapContextCreate:無效數據字節/行:對於8個整數位/組件,3個組件,kCGImageAlphaNoneSkipFirst應該至少爲400。CBitmapContextCreate,CGContextDrawImage,CGBitmapContextCreateImage錯誤

錯誤:CGContextDrawImage:無效的上下文

錯誤:CGBitmapContextCreateImage:無效的上下文

目前,我有在OS 4.0上運行完美的應用程序,但我一直在努力讓它在3.1正常工作.3我不斷收到上面提到的錯誤。

我對iPhone的開發相當陌生,我不完全確定問題是什麼。我正在使用我在另一篇文章中找到的圖像大小調整代碼。

下面是代碼:

- (UIImage*)imageWithImage:(UIImage*)sourceImage scaledToSizeWithSameAspectRatio:(CGSize)targetSize{ 
CGSize imageSize = sourceImage.size; 
CGFloat width = imageSize.width; 
CGFloat height = imageSize.height; 
CGFloat targetWidth = targetSize.width; 
CGFloat targetHeight = targetSize.height; 
CGFloat scaleFactor = 0.0; 
CGFloat scaledWidth = targetWidth; 
CGFloat scaledHeight = targetHeight; 
CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 

if (CGSizeEqualToSize(imageSize, targetSize) == NO) { 
    CGFloat widthFactor = targetWidth/width; 
    CGFloat heightFactor = targetHeight/height; 

    if (widthFactor > heightFactor) { 
     scaleFactor = widthFactor; // scale to fit height 
    } 
    else { 
     scaleFactor = heightFactor; // scale to fit width 
    } 

    scaledWidth = width * scaleFactor; 
    scaledHeight = height * scaleFactor; 

    // center the image 
    if (widthFactor > heightFactor) { 
     thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
    } 
    else if (widthFactor < heightFactor) { 
     thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
    } 
}  

CGImageRef imageRef = [sourceImage CGImage]; 
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); 
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); 

if (bitmapInfo == kCGImageAlphaNone) { 
    bitmapInfo = kCGImageAlphaNoneSkipLast; 
} 

CGContextRef bitmap; 

if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) { 
    bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

} else { 
    bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

}  

// In the right or left cases, we need to switch scaledWidth and scaledHeight, 
// and also the thumbnail point 
if (sourceImage.imageOrientation == UIImageOrientationLeft) { 
    thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x); 
    CGFloat oldScaledWidth = scaledWidth; 
    scaledWidth = scaledHeight; 
    scaledHeight = oldScaledWidth; 

    CGContextRotateCTM (bitmap, radians(90)); 
    CGContextTranslateCTM (bitmap, 0, -targetHeight); 

} else if (sourceImage.imageOrientation == UIImageOrientationRight) { 
    thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x); 
    CGFloat oldScaledWidth = scaledWidth; 
    scaledWidth = scaledHeight; 
    scaledHeight = oldScaledWidth; 

    CGContextRotateCTM (bitmap, radians(-90)); 
    CGContextTranslateCTM (bitmap, -targetWidth, 0); 

} else if (sourceImage.imageOrientation == UIImageOrientationUp) { 
    // NOTHING 
} else if (sourceImage.imageOrientation == UIImageOrientationDown) { 
    CGContextTranslateCTM (bitmap, targetWidth, targetHeight); 
    CGContextRotateCTM (bitmap, radians(-180.)); 
} 

CGContextDrawImage(bitmap, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), imageRef); 
CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
UIImage* newImage = [UIImage imageWithCGImage:ref]; 

CGContextRelease(bitmap); 
CGImageRelease(ref); 

return newImage; 

任何幫助,將不勝感激。如果您需要更多信息,我會很樂意發佈。

回答

0

第一個錯誤是告訴你,將CGImageGetBitsPerComponent(imageRef)作爲新位圖上下文的行字節傳遞是不正確的。行字節數必須至少爲每個像素的字節數的寬度乘以。