MACOS获得 CGContextRef 获得图片数据类型转换 NSImage、CIImage、CGImageRef

将NSImage转换为CIImage

#import <QuartzCore/CIFilter.h>

// convert NSImage to bitmap

NSImage * myImage  = [self currentImage];

NSData  * tiffData = [myImage TIFFRepresentation];

NSBitmapImageRep * bitmap;

bitmap = [NSBitmapImageRep imageRepWithData:tiffData];

// create CIImage from bitmap

CIImage * ciImage = [[CIImage alloc]
initWithBitmapImageRep:bitmap];

// create affine transform to flip CIImage

NSAffineTransform *affineTransform = [NSAffineTransform
transform];

[affineTransform translateXBy:0 yBy:128];

[affineTransform scaleXBy:1 yBy:-1];

// create CIFilter with embedded affine transform

CIFilter *transform = [CIFilter
filterWithName:@"CIAffineTransform"];

[transform setValue:ciImage forKey:@"inputImage"];

[transform setValue:affineTransform
forKey:@"inputTransform"];

// get the new CIImage, flipped and ready to serve

CIImage * result = [transform valueForKey:@"outputImage"];

// draw to view

[result drawAtPoint: NSMakePoint ( 0,0 )

fromRect:
NSMakeRect  ( 0,0,128,128 )

operation:
NSCompositeSourceOver

fraction:
1.0];

// cleanup

[ciImage release];

将CGImageRef转换为NSImage *

- (NSImage*) imageFromCGImageRef:(CGImageRef)image

{

NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0,
0.0);

CGContextRef imageContext = nil;

NSImage* newImage = nil;

// Get the image dimensions.

imageRect.size.height =
CGImageGetHeight(image);

imageRect.size.width =
CGImageGetWidth(image);

// Create a new image to receive the Quartz
image data.

newImage = [[[NSImage alloc]
initWithSize:imageRect.size] autorelease];

[newImage lockFocus];

// Get the Quartz context and draw.

imageContext =
(CGContextRef)[[NSGraphicsContext currentContext]

graphicsPort];

CGContextDrawImage(imageContext,
*(CGRect*)&imageRect, image);

[newImage unlockFocus];

return newImage;

}

将NSImage *转换为CGImageRef

- (CGImageRef)nsImageToCGImageRef:(NSImage*)image;

{

NSData * imageData = [image
TIFFRepresentation];

CGImageRef imageRef;

if(imageData)

{

CGImageSourceRef
imageSource =

CGImageSourceCreateWithData(

(CFDataRef)imageData,  NULL);

imageRef =
CGImageSourceCreateImageAtIndex(

imageSource,
0, NULL);

}

return imageRef;

}

MACOS获得 CGContextRef 获得图片数据类型转换
NSImage、CIImage、CGImageRef,码迷,mamicode.com

MACOS获得 CGContextRef 获得图片数据类型转换
NSImage、CIImage、CGImageRef

时间: 2024-08-06 11:33:27

MACOS获得 CGContextRef 获得图片数据类型转换 NSImage、CIImage、CGImageRef的相关文章

tf.cast()数据类型转换

tf.cast()函数的作用是执行 tensorflow 中张量数据类型转换,比如读入的图片如果是int8类型的,一般在要在训练前把图像的数据格式转换为float32. cast定义: cast(x, dtype, name=None)第一个参数 x:   待转换的数据(张量)第二个参数 dtype: 目标数据类型第三个参数 name: 可选参数,定义操作的名称 int32转换为float32: import tensorflow as tf t1 = tf.Variable([1,2,3,4,

Objective-C数据类型、数据类型转换

数据类型 1.Objective-C数据类型可以分为:基本数据类型.对象数据类型和id类型. 2.基本数据类型有:int.float.double和char类型. 3.对象类型就是类或协议所声明的指针类型,例如: NSAutoreleasePool *pool ,其中,NSAutoreleasePool是一个类,NSAutoreleasePool *是它指针类型或叫对象类型. 4.id类型可以表示任何类型,一般只是表示对象类型,不表示基本数据类型,所以刚才的变量可以声明pool也可以声明为id

数据类型转换-运算符

##基本数据类型转换 - 默认顺序是 * byte->short->int->long->float->double - 自动提升规则(针对二元操作符) * 所有的byte,short,char型会自动提升为int型 * 若一个操作数是long类型,则另一个操作数也会转换为long类型,计算的结果类型也是long类型 * float类型,double类型跟long类型的提升规则一样 - 注意 * 默认浮点型是double类型,默认整型是int类型 * 举例说明: ``` fl

Eclipse-运行符-数据类型转换-环境变量配置

1.能够使用Eclipse快捷键 ctrl + /   单行注释:再按一次则取消: ctrl + shift + /  多行注释:  ctrl + shift + \  取消多行注释: ctrl + shift + f  格式化代码 ctrl + d  删除光标所在行 shift + enter 光标所在行的下方增加一行空白,无论光标在该行的什么位置 ctrl + alt + 向下箭头  向下重复增加该行代码 alt + shift + x  运行代码 alt + / 自动补全  (Syso会自

简谈-Python的输入、输出、运算符、数据类型转换

输出: 格式化输出: 看到了 % 这样的操作符,这就是Python中格式化输出. 换行输出: 在输出的时候,如果有 \n 那么,此时 \n 后的内容会在另外一行显示 输入: 在python2.7当中,使用:raw_input(),在python3中不能使用.Python3只有input() 在python2中的input()把用户输入的东西当成式子(字符)了.所以在python2中一般用raw_input() python3中的input与python2中的raw_input()功能一样 运算符

JavaScript中数据类型转换总结

在js中,数据类型转换分为显式数据类型转换和隐式数据类型转换. 1, 显式数据类型转换 a:转数字: 1)Number转换: 代码: var a = "123"; a = Number(a); 注意: a)如果转换的内容本身就是一个数值类型的字符串,那么将来在转换的时候会返回自己. b)如果转换的内容本身不是一个数值类型的字符串,那么在转换的时候结果是NaN. c)如果要转换的内容是空的字符串,那以转换的结果是0. d)如果是其它的字符,那么将来在转换的时候结果是NaN. 2)pars

Arduino中数据类型转换 float/double转换为char 亲测好使,dtostrf()函数

如何轻松玩转Arduino单片机,当我在进行数据转换的时候,遇到了问题,尝试了C语言和C++中的好多函数,都没有达到将float型数据转换为char型的目的.苦苦查阅资料后,终于找到了一个大神级函数!!!dtostrf(),可以轻松实现数据类型from   float   to  char .            get it 格式如下: char* dtostrf(double _val,signed char _width, unsigned char prec, char* _s) 参数

Arduino中数据类型转换 int转换为char 亲测好使,itoa()函数

由于博主最近在做一个项目,需要采集不同传感器的数据,包括float型的HCHO,以及int型的PM2.5数据.但是最终向服务器上传的数据都得转换为char型才能发送,这是借鉴了一个github上面的实例实现了在Arduino上部署socket使之与服务器进行交互. github实例如下: https://github.com/washo4evr/Socket.io-v1.x-Library 在本项目中多次使用了数据类型转换,前文提到了float和double类型转换为char,如下:http:/

SpringMVC框架下数据的增删改查,数据类型转换,数据格式化,数据校验,错误输入的消息回显

在eclipse中javaEE环境下: 这儿并没有连接数据库,而是将数据存放在map集合中: 将各种架包导入lib下... web.xml文件配置为 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/