如何通过CGImage得到bitmap
来源:互联网 发布:卖家淘宝试用中心 编辑:程序博客网 时间:2024/06/05 15:34
在目前(iOS4.3版本已经出来了)cocoa没有提供从CGimage到bitmap的接口,想得到bitmap还得费一番周折。以下是我从苹果开发者网站上找到的一段,可以实现。大体思路为:先创建满足自己要求的Context,再将原图片画到Context上,然后再从Context上得到图片的bitmap数据。 原文:http://developer.apple.com/library/ios/#qa/qa1509/_index.html creates a bitmap context with a 8-bits per component ARGB color space, draws the source image to this context, then retrieves the image bits in this color space from the context. Regardless of what the source image format is (CMYK, 24-bit RGB, Grayscale, and so on) it will be converted over to this color space. For more information about creating bitmap contexts for other pixel formats, see the Quartz 2D Programming Guide. Listing 2 Accessing the pixel data of a CGImage
.void ManipulateImagePixelData(CGImageRef inImage){ // Create the bitmap context CGContextRef cgctx = CreateARGBBitmapContext(inImage); if (cgctx == NULL) { // error creating context return; } // Get image width, height. We'll use the entire image. size_t w = CGImageGetWidth(inImage); size_t h = CGImageGetHeight(inImage); CGRect rect = {{0,0},{w,h}}; // Draw the image to the bitmap context. Once we draw, the memory // allocated for the context for rendering will then contain the // raw image data in the specified color space. CGContextDrawImage(cgctx, rect, inImage); // Now we can get a pointer to the image data associated with the bitmap // context. void *data = CGBitmapContextGetData (cgctx); if (data != NULL) { // **** You have a pointer to the image data **** // **** Do stuff with the data here **** } // When finished, release the context CGContextRelease(cgctx); // Free image data memory for the context if (data) { free(data); }}CGContextRef CreateARGBBitmapContext (CGImageRef inImage){ CGContextRef context = NULL; CGColorSpaceRef colorSpace; void * bitmapData; int bitmapByteCount; int bitmapBytesPerRow; // Get image width, height. We'll use the entire image. size_t pixelsWide = CGImageGetWidth(inImage); size_t pixelsHigh = CGImageGetHeight(inImage); // Declare the number of bytes per row. Each pixel in the bitmap in this // example is represented by 4 bytes; 8 bits each of red, green, blue, and // alpha. bitmapBytesPerRow = (pixelsWide * 4); bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); // Use the generic RGB color space. colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); if (colorSpace == NULL) { fprintf(stderr, "Error allocating color space/n"); return NULL; } // Allocate memory for image data. This is the destination in memory // where any drawing to the bitmap context will be rendered. bitmapData = malloc( bitmapByteCount ); if (bitmapData == NULL) { fprintf (stderr, "Memory not allocated!"); CGColorSpaceRelease( colorSpace ); return NULL; } // Create the bitmap context. We want pre-multiplied ARGB, 8-bits // per component. Regardless of what the source image format is // (CMYK, Grayscale, and so on) it will be converted over to the format // specified here by CGBitmapContextCreate. context = CGBitmapContextCreate (bitmapData, pixelsWide, pixelsHigh, 8, // bits per component bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst); if (context == NULL) { free (bitmapData); fprintf (stderr, "Context not created!"); } // Make sure and release colorspace before returning CGColorSpaceRelease( colorSpace ); return context;}
Refere
- 如何通过CGImage得到bitmap
- 通过图片url,得到Bitmap对象
- 如何从HBITMAP类型得到BITMAP类型
- 如何通过gps得到当前位置
- 如何得到bitmap某一点像素的RGB值
- 如何得到bitmap某一点像素的RGB值
- 如何通过搜索引擎得到更多的流量
- 如何通过JS得到访问者的IP
- 得到一个BitMap对象
- drawingcache解析 通过view的绘制缓存得到bitmap,从而实现view内容截图
- CGImage载入的图片,查看bitmap详细信息的一段好用代码
- ios--CGImage载入的图片,查看bitmap详细信息的一段好用代码
- CGImage载入的图片,查看bitmap详细信息的一段好用代码
- CGImage载入的图片,查看bitmap详细信息的一段好用代码
- CGImage载入的图片,查看bitmap详细信息的一段好用代码
- 如何通过托盘图标得到该所属进程
- 了解如何通过reverse_iterator的base得到iterator
- 教你如何通过Mysql弱口令得到系统权限
- DoDataExchange和UpdateData()--MFC函数
- liunx下安装jdk
- size_t,__T,_T,TEXT,_TEXT等一些特殊宏的理解
- cg数据类型
- 在Visual Assist X中增加Doxygen格式的注解的模板 [RT,修改]
- 如何通过CGImage得到bitmap
- 打算长期入住这里了,求学习。。。。
- C++学习总结_头文件与源文件
- struts2.1.8.1+hibernate3.5.6Final的分页
- uboot中的TEXT_BASE
- 一个显示时间的ClockPanel
- PHP输出Hello world!之另类实现
- datatable 遍历
- bootloader与linux中位置无关代码(PIC)的分析理解