图片数据类型转换 NSImage、CIImage、CGImageRef
发布日期:2021-07-21 16:13:52 浏览次数:12 分类:技术文章

本文共 2267 字,大约阅读时间需要 7 分钟。

不知道苹果怎么想的,七八种图片数据类型格式,转换起来非常麻烦。本文摘抄了几种转换的方法,供参考。 将NSImage转换为CIImage #import QuartzCore/CIFilter.h // convert NSImage to bitmap NSImage *

不知道苹果怎么想的,七八种图片数据类型格式,转换起来非常麻烦。本文摘抄了几种转换的方法,供参考。

将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;

}

转载地址:https://blog.csdn.net/love_hot_girl/article/details/6962386 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iOS的绘图机制-iOS,iPad,iPhone,Objective-c
下一篇:九型人格Enneagram

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年08月19日 08时30分02秒