iphone下bmp读取显示两种方法

  1. #define BMP_SIZE   320*480*3  
  2. #define BMP_HEADER_LENGTH 54  
  3.    
  4. //方法一:  
  5. CGImageRef GetImageData()  
  6. {  
  7.     unsigned char* pData=NULL;  
  8.     int i = 0;  
  9.    
  10.     int bitsPerComponent = 8;  
  11.     int bitsPerPixel = 24;  
  12.     int bytesPerRow = 3 * SCREEN_METRIC_WIDTH;  
  13.     NSInteger myLCDDataLength =   
  14. SCREEN_METRIC_WIDTH * SCREEN_METRIC_HEIGHT * 2;  
  15.     CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();  
  16.     CGBitmapInfo bmpInof = kCGBitmapByteOrderDefault;  
  17.     CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;  
  18.     CGDataProviderRef provider = nil;  
  19.     void*pBuffer = malloc(BMP_SIZE + BMP_HEADER_LENGTH);    
  20.     for(pData = pBuffer, i = 0; i < SCREEN_METRIC_WIDTH * SCREEN_METRIC_HEIGHT; ++i)  
  21.     {  
  22.         *pData++ = 0xFF; //R  
  23.         *pData++ = 0x0; //G  
  24.         *pData++ = 0x0; //B  
  25.     }  
  26.    
  27.     provider = CGDataProviderCreateWithData(NULL, pBuffer, myLCDDataLength, NULL);  
  28.    
  29.     return CGImageCreate(SCREEN_METRIC_WIDTH,SCREEN_METRIC_HEIGHT,  
  30.                          bitsPerComponent,  
  31.                          bitsPerPixel,  
  32.                          bytesPerRow,  
  33.                          colorSpaceRef,  
  34.                          bmpInof,  
  35.                          provider,  
  36.                          NULL,   
  37.                          NO,  
  38.                          renderingIntent);      
  39. }  
  40.    
  41. void GUI_DrawBMP()  
  42. {  
  43.     CGImageRef m_cgImage = GetImageData();  
  44.    
  45.     pUIImage = [ [ UIImage alloc] initWithCGImage:m_cgImage];  
  46.    
  47.     [pUIImage drawAtPoint:CGPointMake(0.0f,0.0f)];  
  48.    
  49.     [pUIImage dealloc];  
  50.    
  51. }  
  52.    
  53. //方法二:  
  54. const char* const g_pszFilePath = "/example.bmp";  
  55. FILE * pFile = fopen(g_pszFilePath, "rb");  
  56. if(NULL != pFile)  
  57. {  
  58.     void*pBuffer = malloc(BMP_SIZE + BMP_HEADER_LENGTH);     
  59.     if (NULL != pBuffer)  
  60.     {  
  61.     fseek(pFile , 0, SEEK_SET);  
  62.     fread(pBuffer, 1, 54, pFile);  
  63.     fclose(pFile);  
  64.     memset(pBuffer + BMP_HEADER_LENGTH, BMP_SIZE);  
  65.     NSData* pNSData = [ [NSData alloc] initWithBytes:pBuffer   
  66. length: BMP_SIZE + BMP_HEADER_LENGTH];  
  67.     UIImage* pUIImage = [ [ UIImage alloc] initWithData:myNSData];  
  68.     [pUIImage drawAtPoint:CGPointMake(0.0f,0.0f)];  
  69.     [pNSData dealloc];   
  70.     free(pBuffer);  
  71.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值