Get size of a UIImage (bytes length) not height and width

后端 未结 10 2130
说谎
说谎 2020-11-28 09:00

I\'m trying to get the length of a UIImage. Not the width or height of the image, but the size of the data.

10条回答
  •  生来不讨喜
    2020-11-28 09:50

    Use the CGImage property of UIImage. Then using a combination of CGImageGetBytesPerRow *
    CGImageGetHeight, add in the sizeof UIImage, you should be within a few bytes of the actual size.

    This will return the size of the image, uncompressed, if you want to use it for purposes such as malloc in preparation for bitmap manipulation (assuming a 4 byte pixel format of 3 bytes for RGB and 1 for Alpha):

    int height = image.size.height,
        width = image.size.width;
    int bytesPerRow = 4*width;
    if (bytesPerRow % 16)
        bytesPerRow = ((bytesPerRow / 16) + 1) * 16;
    int dataSize = height*bytesPerRow;
    

提交回复
热议问题