OC 常用图像格式之间转换

1.CGImageRef to NSImage

CGImageRef cgImage;
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
// Create an NSImage and add the bitmap rep to it...
NSImage *image = [[NSImage alloc] init];
[image addRepresentation:bitmapRep];

2.NSImage to NSData

NSImage *image;
NSData *imageData = [image TIFFRepresentation];

3.从CGImageRef获取ARGB数据的几种方式:

//3.1使用CGDataProviderCopyData 
CGImageRef cgImage;
CFDataRef dataFromCGImageProvider = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));
GLubyte  *imageData = (GLubyte *)CFDataGetBytePtr(dataFromCGImageProvider);


//3.2使用context draw image
CGImageRef cgImage;
size_t width =  CGImageGetWidth(cgImage);
size_t height = CGImageGetHeight(cgImage);
size_t bytesPerRow = CGImageGetBytesPerRow(cgImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * bytesPerRow, sizeof(unsigned char));
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                    bitsPerComponent, bytesPerRow, colorSpace,
                    kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage);
uint8_t* rgbaData = (uint8_t*)CGBitmapContextGetData(context);
CGContextRelease(context);


//3.3 先转NSImage,再转NSData,再获取数据
CGImageRef cgImage;
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
    // Create an NSImage and add the bitmap rep to it...
NSImage *image = [[NSImage alloc] init];
[image addRepresentation:bitmapRep];
NSData *imageData = [image TIFFRepresentation];
uint8_t* rgbaData = (uint8_t*)imageData.bytes;

4.NSData和YUV CVPixelBuffer之间的转换

//1.YUV CVPixelBuffer转NSData
- (NSData* ) YUVBufferToData:(CVPixelBufferRef)YUVpixelBuffer
{
    NSMutableData* YUVMutData;
    
    CVPixelBufferLockBaseAddress(YUVpixelBuffer, 0);
    unsigned char* yBaseAddress = (unsigned char*)CVPixelBufferGetBaseAddressOfPlane(YUVpixelBuffer, 0);
    int32_t yBytesPerRow = (int32_t)CVPixelBufferGetBytesPerRowOfPlane(YUVpixelBuffer, 0);
    int32_t width __unused = (int32_t)CVPixelBufferGetWidth(YUVpixelBuffer);
    int32_t height = (int32_t)CVPixelBufferGetHeight(YUVpixelBuffer);
    int32_t yLength = yBytesPerRow*height;
    YUVMutData = [NSMutableData dataWithBytes: yBaseAddress length: yLength];
    
    unsigned char* uvBaseAddress = (unsigned char*)CVPixelBufferGetBaseAddressOfPlane(YUVpixelBuffer, 1);
    int32_t uvBytesPerRow = (int32_t)CVPixelBufferGetBytesPerRowOfPlane(YUVpixelBuffer, 1);
    int32_t uvLength = uvBytesPerRow*height/2;
    NSMutableData* UVData = [NSMutableData dataWithBytes: uvBaseAddress length: uvLength];
    [YUVMutData appendData:UVData];
    NSData* YUVData = [NSData dataWithData:YUVMutData];
    CVPixelBufferUnlockBaseAddress(YUVpixelBuffer, 0);
    return YUVData;
}

//2.NSData转CVPixelBuffer
- (void) DataToYUVBuffer:(NSData*)YUVData width:(int32_t)YUVWidth height:(int32_t)YUVHeight
{
    unsigned char* srcPtr = (unsigned char*)YUVData.bytes;
    CVPixelBufferLockBaseAddress(_pixelBufferYUV2, 0);
    unsigned char* yBaseAddress = (unsigned char*)CVPixelBufferGetBaseAddressOfPlane(_pixelBufferYUV2, 0);
    unsigned char* uvBaseAddress = (unsigned char*)CVPixelBufferGetBaseAddressOfPlane(_pixelBufferYUV2, 1);
    int32_t yLength = YUVWidth*YUVHeight;
    int32_t uvLength = YUVWidth*YUVHeight/2;
    memcpy(yBaseAddress, srcPtr, yLength);
    memcpy(uvBaseAddress, srcPtr+yLength, uvLength);
    CVPixelBufferUnlockBaseAddress(_pixelBufferYUV2, 0);
    //return _pixelBufferYUV2;
}


4. NSImage转CGImageRef

+ (CGImageRef)getCGImageRefFromNSImage:(NSImage *)image {

    NSData * imageData = [image TIFFRepresentation];
    CGImageRef imageRef = nil;
    if(imageData) {
        CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData,  NULL);

        imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
    }
    return imageRef;
}

//the other way
NSImage *image
NSBitmapImageRep *imageClass = [[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];
CGImageRef cgImage = imageClass.CGImage;

5. CGImage 转 unsigned char*

CGImageRef cgImage;

size_t width =  CGImageGetWidth(cgImage);
size_t height = CGImageGetHeight(cgImage);
size_t bytesPerRow = CGImageGetBytesPerRow(cgImage);

CFDataRef dataFromCGImageProvider = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));
GLubyte  *imageData = (GLubyte *)CFDataGetBytePtr(dataFromCGImageProvider);

6. 也可参考 https://www.jianshu.com/p/949b8b1075d8

7.保存图像到本地

NSCursor* nscursor = [NSCursor currentSystemCursor];
NSImage* nsimage __unused = [nscursor image];
[nsimage lockFocus];
NSBitmapImageRep *imageRepresentation = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0,0, nsimage.size.width, nsimage.size.height)];
[nsimage unlockFocus];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:0] forKey:NSImageCompressionFactor];
NSData *data = [imageRepresentation representationUsingType:NSPNGFileType properties:imageProps];
[data writeToFile:@"~/Documents/cursor.png" atomically:YES];

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值