zt9788
zt9788

Reputation: 958

IOS CGBitmapContextCreate Large bitmap

CGContextRef context =  CGBitmapContextCreate(nil,
        width, //if width More than 6002/4
        height, 
        8,
        width*4,//if width*4 > 6002
        colorSpace,
        kCGImageAlphaPremultipliedFirst |kCGBitmapByteOrder32Little );

i want to build a large bitmap (width <= 2500) when width*4>6002 has a error for that

<Error>: CGBitmapContextCreate: unsupported parameter combination: 
8 integer bits/component;  32 bits/pixel; 
3-component color space; kCGImageAlphaPremultipliedFirst; 6002 bytes/row.

how to build a large bitmap thanks.

Upvotes: 1

Views: 822

Answers (2)

Tammo Freese
Tammo Freese

Reputation: 10754

The problem is the 6002 bytes / row, since each pixel needs 4 bytes here, but 6002 is not dividable by 4 without remainder. Better calculate the rows per pixel:

size_t width = 1920;
size_t height = 1080;
CGContextRef context = CGBitmapContextCreate(
    NULL,
    width,
    height,
    8,
    width * 4,
    colorSpace,
    kCGImageAlphaPremultipliedFirst |kCGBitmapByteOrder32Little );

Upvotes: 1

DivineDesert
DivineDesert

Reputation: 6954

new bytesPerRow will be different from the original image. You need to calc the new bytesPerRow.

bytesPerPixel * targetWidth

You can't take static 8 and 4.

Refer this for Color space and relative bytesPerPixel.

Upvotes: 0

Related Questions