码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-07-22 23:16:13      阅读:586      评论:0      收藏:0      [点我收藏+]

标签:os   strong   数据   width   io   art   

 

 

 将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

标签:os   strong   数据   width   io   art   

原文地址:http://www.cnblogs.com/spiderdzl/p/3699776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!