- #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);
- }
- }
iphone下bmp读取显示两种方法
最新推荐文章于 2022-07-06 23:33:56 发布