2010-06-02 5 views
2

오류 : CGBitmapContextCreate : 잘못된 데이터 바이트/행 : 8 정수 비트/구성 요소, 3 구성 요소, kCGImageAlphaNoneSkipFirst에 대해 최소 400이어야합니다.CBitmapContextCreate, CGContextDrawImage, CGBitmapContextCreateImage의 오류

오류 : CGContextDrawImage : 잘못된 컨텍스트

오류 : CGBitmapContextCreateImage : 유효하지 않은 상황

현재, 나는 OS 4.0에서 완벽하게 실행되는 응용 프로그램,하지만 나는 그것이 3.1에서 제대로 작동하도록하기 위해 노력 해왔다 .3 그리고 위에서 언급 한 오류가 계속 발생합니다.

저는 iPhone 개발에 새로운 경험이 있으며 문제가 무엇인지 정확하게 알지 못합니다. 내가 stackoverflow에 다른 게시물에서 찾은 이미지 크기 조정 코드를 사용하고 있습니다. 어떤 도움을 주시면 감사하겠습니다

- (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)을 새 비트 맵 컨텍스트의 행 바이트로 전달하는 것이 맞지 않는다는 것을 알려줍니다. 행 바이트 수는 적어도 너비와 픽셀 당 바이트 수를 곱한 값이어야합니다.