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