Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGBitmapContextCreate: invalid data bytes/row

Tags:

ios

I am trying to resize an image before displaying it back to the user.

The error I am getting is Error: CGBitmapContextCreate: invalid data bytes/row: should be at least 5504 for 8 integer bits/component, 3 components, kCGImageAlphaNoneSkipFirst.

I also get Error: CGContextDrawImage: invalid context 0x0.

This is the code I'm using:

    -(UIImage*)imageByScalingToSize:(CGRect)target :(UIImage*)old
{
    UIImage* sourceImage = old;
    CGFloat targetWidth = target.size.width + 10;
    CGFloat targetHeight = target.size.height;
    CGFloat x = target.origin.x;
    CGFloat y = target.origin.y - 105;

    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);      
    }

    if (sourceImage.imageOrientation == UIImageOrientationLeft) {
        CGContextRotateCTM (bitmap, radians(90));
        CGContextTranslateCTM (bitmap, 0, -targetHeight);     
    } 
    else if (sourceImage.imageOrientation == UIImageOrientationRight) {
        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(x, y, targetWidth, targetHeight), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage* newImage = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return newImage; 
}
like image 576
akandola Avatar asked Jun 09 '14 16:06

akandola


1 Answers

Try this:

bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), 0, colorSpaceInfo, bitmapInfo);    

bitsPerComponent

The number of bits to use for each component of a pixel in memory. For example, for a 32-bit pixel format and an RGB color space, you would specify a value of 8 bits per component. For the list of supported pixel formats, see “Supported Pixel Formats” in the “Graphics Contexts” chapter of Quartz 2D Programming Guide.

If this does not improve things, please post the values for CGImageGetBitsPerComponent(imageRef) and CGImageGetBytesPerRow(imageRef).

like image 185
sergio Avatar answered Oct 09 '22 01:10

sergio