iOS如何将RGB565的原始图像数据转为UIImage对象
2014-10-30 17:21
555 查看
我们在做一些图像处理时,往往会涉及到RGB565这种图像数据格式。由于其每个像素仅占2个字节,对于不需要像素透明度的情况下使用RGB565既能基本能保证图像的色彩,又能降低图像数据尺寸,节省带宽。因此,RGB565将会是一种常用的比较经济的图像处理的格式。
下面就来描述一下如何在iOS中将一段RGB565的原始图像数据转为UIImage对象。见以下代码:
iOS中,QuartzCore支持的16位RGB就一种格式——AlphaNoneSkipFirst,每个分量5比特,每个像素16比特,字节序为ByteOrder16Little。因此,R分量位于低字节;而B分量位于高字节。下面举个应用例子:
以上代码创建了一幅128x128的RGB565的图像,上64行为红色;下64行为蓝色。
下面就来描述一下如何在iOS中将一段RGB565的原始图像数据转为UIImage对象。见以下代码:
- (UIImage*)imageFromRGB565:(void*)rawData width:(int)width height:(int)height { const size_t bufferLength = width * height * 2; NSData *data = [NSData dataWithBytes:rawData length:bufferLength]; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data); // Creating CGImage from cv::Mat CGImageRef imageRef = CGImageCreate(width, //width height, //height 5, //bits per component 16, //bits per pixel width * 2, //bytesPerRow colorSpace, //colorspace kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder16Little,// bitmap info provider, //CGDataProviderRef NULL, //decode false, //should interpolate kCGRenderingIntentDefault //intent ); // Getting UIImage from CGImage UIImage *finalImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); CGDataProviderRelease(provider); CGColorSpaceRelease(colorSpace); return finalImage; }
iOS中,QuartzCore支持的16位RGB就一种格式——AlphaNoneSkipFirst,每个分量5比特,每个像素16比特,字节序为ByteOrder16Little。因此,R分量位于低字节;而B分量位于高字节。下面举个应用例子:
- (void)buttonTouched:(id)sender { unsigned short *imageBuffer = (unsigned short*)malloc(128 * 128 * 2); for(int row = 0; row < 128; row++) { unsigned short color = 0x001f; if(row >= 64) color = 0xf800; for(int col = 0; col < 128; col++) imageBuffer[row * 128 + col] = color; } UIImage *image = [self imageFromRGB565:imageBuffer width:128 height:128]; free(imageBuffer); UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 128.0f) * 0.5f, (self.view.frame.size.height - 128.0f) * 0.5f, 128.0f, 128.0f)]; imageView.image = image; [self.view addSubview:imageView]; [imageView release]; }
以上代码创建了一幅128x128的RGB565的图像,上64行为红色;下64行为蓝色。
相关文章推荐
- iOS如何将RGB565的原始图像数据转为UIImage对象
- 如何将ImagXpress对象的图像数据转移到Barcode Xpress对象上
- rgb原始数据转为uiimage
- 把iOS的UIImage转为cocos2dx中可用的图片数据
- iOS本地数据存储都有哪几种方式?iOS如何实现复杂对象的存储?
- iOS 演示 Modal Segue 的使用,包括参数如何从呈现者传递到 ModalView 以及数据如何从 ModalView 传回呈现者(利用委托对象)
- 如何做YUV的分辨率转换 - FFMPEG 实现 YUV,RGB各种图像原始数据之间的转换(swscale)
- OpenCV显示图像--原始图像数据到Mat对象的转换函数
- iOS开发的一些小技术:让UIImage有缩放功能、控制log的输出、xcode修改文件注释、随机数的使用、在UIImageView 中旋转图像、在Quartz中如何设置旋转点、创建.plist文件并存储
- iOS 6编程-UIImage如何从URL加载图像?
- ios-day12-04(ios开发中数据存储之NSKeyedArchiver归档(NSCoding)、普通对象如何归档、普通对象的子类如何归档)
- 如何防止XMLHttpRequest对象传回的中文数据中出现乱码?
- 如何使用 Visual C# 2005 或 Visual C# .NET 通过 DataSet 对象更新数据库
- 如何在已经绑定数据的dataGridView对象中动态添加记录?
- 如何采集原始图象数据存储格式的数据
- 将 html 中的 table 转为数据对象。
- 如何以对象的形式存储数据
- 小技巧:用CxImage将从摄像头获取的图像原始数据转成JPG,然
- Shell数据对象(三):目标如何处理数据对象
- 如何将jpg格式图像文件转化成一系列二进制数据,又如何将此二进制数据转化成jpg格式的文件?