iPhone CGContextRef CGBitmapContextCreate unsupported parameter combination

前端 未结 5 798
抹茶落季
抹茶落季 2020-12-06 11:45

In my application I need to resize and crop some images, stored locally and online. I am using Trevor Harmon\'s tutorial which implements UIImage+Resize.

<
相关标签:
5条回答
  • 2020-12-06 12:08

    I figured out it's a problem with the color space. Just replace CGImageGetColorSpace(imageRef) with CGColorSpaceCreateDeviceRGB() . This works for me when trying to save image I got from AVCaptureSession. And don't forget to release it!

    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                                newRect.size.width,
                                                newRect.size.height,
                                                CGImageGetBitsPerComponent(imageRef),
                                                0,
                                                rgbColorSpace,//CGImageGetColorSpace(imageRef), sometimes contains unsupported colorspace
                                                bitmapInfo);
    CGColorSpaceRelease(rgbColorSpace);
    
    0 讨论(0)
  • 2020-12-06 12:12

    Ok this may or may not help you (and i appreciate that this is an old post)

    I was getting a similar problem because the width parameter (in your case newRect.size.width) had a decimal fraction component (eg 100.001 instead of 100.0). i typecast it to an integer and back, truncating the decimals component, and the problem went away. i am guessing there is a test to see that the number of bits per component x pixels etc adds up, and it can't deal with fractional pixels/points. you are welcome to use this method if it helps.

    +(CGSize)  fixSize:(CGSize) forSize{
        NSInteger w = (NSInteger) forSize.width;
        NSInteger h = (NSInteger) forSize.height;
        return CGSizeMake(w, h);
    }
    
    0 讨论(0)
  • 2020-12-06 12:15

    I had the same problem with iOS 5 simulator and the answers above didn't resolve my issue: images were not loaded and the console still reported the same errors.

    I am using the very popular categories found here.

    On this blog people are having the same issue(s). Matt's answer from November 22, 2011 helped me.

    Cheers!

    0 讨论(0)
  • 2020-12-06 12:30

    I was seeing this issue in the simulator when switching from 5.1 to 6.1 for testing. Closing the simulator and opening it again seems to have removed the error.

    0 讨论(0)
  • 2020-12-06 12:31

    Replying here since I had the exact same pixel format when I got this error. I hope this answer helps someone.

    The reason it was failing, in my case, was that kCGImageAlphaLast isn't a permitted value anymore on iOS 8, although it works well on iOS 7. The 32 pbb, 8 bpc combination only allows kCGImageAlphaNoneSkip* and kCGImageAlphaPremultiplied* for the Alpha Info. Apparently this was a problem always, but wasn't enforced before iOS 8. Here's my solution:

    - (CGBitmapInfo)normalizeBitmapInfo:(CGBitmapInfo)oldBitmapInfo {
        //extract the alpha info by resetting everything else
        CGImageAlphaInfo alphaInfo = oldBitmapInfo & kCGBitmapAlphaInfoMask;
    
        //Since iOS8 it's not allowed anymore to create contexts with unmultiplied Alpha info
        if (alphaInfo == kCGImageAlphaLast) {
            alphaInfo = kCGImageAlphaPremultipliedLast;
        }
        if (alphaInfo == kCGImageAlphaFirst) {
            alphaInfo = kCGImageAlphaPremultipliedFirst;
        }
    
        //reset the bits
        CGBitmapInfo newBitmapInfo = oldBitmapInfo & ~kCGBitmapAlphaInfoMask;
    
        //set the bits to the new alphaInfo
        newBitmapInfo |= alphaInfo;
    
        return newBitmapInfo;
    }
    

    In my case the failing piece of code looked like this, where imageRef is a CGImageRef of a PNG loaded from the app bundle:

    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                                    newRect.size.width,
                                                    newRect.size.height,
                                                    CGImageGetBitsPerComponent(imageRef),
                                                    0,
                                                    CGImageGetColorSpace(imageRef),
                                                    CGImageGetBitmapInfo(imageRef));
    

    Sources: https://stackoverflow.com/a/19345325/3099609

    https://developer.apple.com/library/mac/DOCUMENTATION/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203-BCIBHHBB

    0 讨论(0)
提交回复
热议问题