0
回答
iphone下bmp读取交显示的两种方法
科大讯飞通用文字识别100000次/天免费使用。立即申请   

虽然在iphone下很少使用bmp格式的图片,但在此要介绍两种方法,让对bmp情有独钟的人,了确心中愿望。

 

#define BMP_SIZE   320*480*3
#define BMP_HEADER_LENGTH 54
 
//方法一:
CGImageRef GetImageData()
{
    unsigned char* pData=NULL;
    int i = 0;
 
    int bitsPerComponent = 8;
    int bitsPerPixel = 24;
    int bytesPerRow = 3 * SCREEN_METRIC_WIDTH;
    NSInteger myLCDDataLength = 
SCREEN_METRIC_WIDTH * SCREEN_METRIC_HEIGHT * 2;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bmpInof = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    CGDataProviderRef provider = nil;
    void*pBuffer = malloc(BMP_SIZE + BMP_HEADER_LENGTH);  
    for(pData = pBuffer, i = 0; i < SCREEN_METRIC_WIDTH * SCREEN_METRIC_HEIGHT; ++i)
    {
        *pData++ = 0xFF; //R
        *pData++ = 0x0; //G
        *pData++ = 0x0; //B
    }
 
    provider = CGDataProviderCreateWithData(NULL, pBuffer, myLCDDataLength, NULL);
 
    return CGImageCreate(SCREEN_METRIC_WIDTH,SCREEN_METRIC_HEIGHT,
                         bitsPerComponent,
                         bitsPerPixel,
                         bytesPerRow,
                         colorSpaceRef,
                         bmpInof,
                         provider,
                         NULL, 
                         NO,
                         renderingIntent);    
}
 
void GUI_DrawBMP()
{
    CGImageRef m_cgImage = GetImageData();
 
    pUIImage = [ [ UIImage alloc] initWithCGImage:m_cgImage];
 
    [pUIImage drawAtPoint:CGPointMake(0.0f,0.0f)];
 
    [pUIImage dealloc];
 
}
 
//方法二:
const char* const g_pszFilePath = "/example.bmp";
FILE * pFile = fopen(g_pszFilePath, "rb");
if(NULL != pFile)
{
    void*pBuffer = malloc(BMP_SIZE + BMP_HEADER_LENGTH);   
    if (NULL != pBuffer)
    {
    fseek(pFile , 0, SEEK_SET);
    fread(pBuffer, 1, 54, pFile);
    fclose(pFile);
    memset(pBuffer + BMP_HEADER_LENGTH, BMP_SIZE);
    NSData* pNSData = [ [NSData alloc] initWithBytes:pBuffer 
length: BMP_SIZE + BMP_HEADER_LENGTH];
    UIImage* pUIImage = [ [ UIImage alloc] initWithData:myNSData];
    [pUIImage drawAtPoint:CGPointMake(0.0f,0.0f)];
    [pNSData dealloc]; 
    free(pBuffer);
    }
}
 


原文链接:http://blog.csdn.net/favormm/article/details/5199218
<无标签>
举报
长平狐
发帖于6年前 0回/213阅
顶部