iPhone开发应用中如何使BMP读取交显示解决方法是本文要介绍的内容,主要是来学习在iphone开发中,BMP显示的问题,本文介绍了两种解决方法。内容不多,主要是基于代码来实现内容。虽然在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);
- }
- }
小结:iPhone开发应用中如何使BMP读取交显示解决方法的内容介绍完了,希望通过本文的学习鞥对你有所帮助!