2010-06-02 22 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)作为新位图上下文的行字节传递是不正确的。行字节数必须至少为每个像素的字节数的宽度乘以。