Mac OS CSV文件解析 CIImage、CGImage、NSImage类型转换

因为业务需求,所以做一个从CSV文件中读取字符串,转换成二维码的小功能.没有什么难点.但是在转换类型的时候,耽搁了一下,查了些文章,最后解决了问题,个人整理用于备用、分享.(如有不对的地方,请在评论中指出)


  • 1.CSV文件的解析

Example:

    //自定义类解析csv文件
    CSVParser *parser=[[CSVParser alloc] init]; 
    
    [parser openFile:path];
    
    NSMutableArray *array=[parser parseFile];
    
    [parser closeFile];
    
    NSLog(@"array %@",array);
    
    for (NSArray * subArray in array) {
        
        if (subArray.length > 0) {
        
            NSLog(@"subArray %@",subArray);
           
        }
        
    }
    

Result:
通过这个类获取到的是一个数组array,array中存的对象subAarray分别对应csv文件中的每一行,

subArray中的对象分别对应这一行的每一列.


  • 2.CIImage、CGImage、NSImage3种类型的转换

关于这3种类型的其他比较简单的转换,就不介绍了.

CIImage官方说明 

Although a CIImage object has image data associated with it, it is not an image. You can think of a CIImage object as an image “recipe.” A CIImage object has all the information necessary to produce an image, but Core Image doesn’t actually render an image until it is told to do so. This “lazy evaluation” method allows Core Image to operate as efficiently as possible.

CIContext and CIImage objects are immutable, which means each can be shared safely among threads. Multiple threads can use the same GPU or CPU CIContext object to render CIImage objects. However, this is not the case for CIFilter objects, which are mutable. A CIFilter object cannot be shared safely among threads. If you app is multithreaded, each thread must create its own CIFilter objects. Otherwise, your app could behave unexpectedly.

虽然CIImage对象具有与其关联的图像数据,但它不是图像。您可以将CIImage对象视为图像“配方”.CIImage对象具有生成映像所需的所有信息,但Core Image实际上不会渲染图像,直到它被告知这样做。这种“延迟评估”方法允许Core Image尽可能高效地运行。
CIContext和CIImage对象是不可变的,这意味着每个线程可以安全地共享。多线程可以使用相同的GPU或CPU CIContext对象来渲染CIImage对象。但是,这不是CIFilter<>对象的情况,这是可变的。 CIFilter对象不能在线程之间安全共享。如果你的应用程序是多线程的,每个线程必须创建自己的CIFilter对象。否则,您的应用可能会出现意外。


// CIImage 转换为 CGImageRef
- (CGImageRef)CGImageRefFromCIImage:(CIImage *)ciImage {
    
    CIContext * ciContext = [[CIContext alloc]init];
    
    CGImageRef cgImage = [ciContext createCGImage:ciImage fromRect:ciImage.extent];
    
    return cgImage;
    
}


//CGImageRef 转换为 NSImage
- (NSImage*)imageFromCGImageRef:(CGImageRef)image{

    
    NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
    
    CGContextRef imageContext = nil;
    
    NSImage* newImage = nil;
    
    imageRect.size.height = CGImageGetHeight(image);
    
    imageRect.size.width = CGImageGetWidth(image);
    
    newImage = [[NSImage alloc] initWithSize:imageRect.size];
    
    [newImage lockFocus];
        
    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;
    
}

//NSImage绘制 位图
- (void)convertNSImageToBitmap:(NSImage *)image {
    
    NSData  * tiffData = [image 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];

}

你可能感兴趣的:(Mac OS CSV文件解析 CIImage、CGImage、NSImage类型转换)