2013-04-15 34 views
0

我想將圖像從一種格式(.png)轉換爲其他圖像格式(.img格式)。我能夠檢索和修改相同圖像格式的rgba值。是否有任何額外的事情我需要做的將其轉換爲其他圖像格式?如何在iOS中創建和檢索新的位圖

我創建了一個空位圖,並想繪製圖像這個位圖。

CGImageRef cgimage = image.CGImage; 

size_t width = CGImageGetWidth(cgimage); 
size_t height = CGImageGetHeight(cgimage); 

size_t bytesPerRow = CGImageGetBytesPerRow(cgimage); 
size_t bytesPerPixel = CGImageGetBitsPerPixel(cgimage); 
size_t bitsPerComponent = CGImageGetBitsPerComponent(cgimage); 
size_t bytes_per_pixel = bytesPerPixel/bitsPerComponent; 

CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(cgimage); 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
unsigned char *rawData = malloc(height * width * 4); 
memset(rawData, 0, height * width * 4); 

CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo); 

CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage); 

iOS中是否有任何函數用輸入的位圖填充修改過的rgba值。

+0

你是什麼意思「如何在iOS中將數據繪製到位圖」?你是否正在討論在特定點採用像素選取顏色? –

+0

@TBlue:修改標題 – Ram

+0

我讀得越多,我越感到困惑。您指定的擴展名之一(.img)直到十年前才被用作光盤映像格式。 –

回答

2

這將是這個樣子:

UIImage *image = self.theImage; 
CGImageRef imageRef = [image CGImage]; 
NSUInteger width = CGImageGetWidth(imageRef); 
NSUInteger height = CGImageGetHeight(imageRef); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
NSMutableData *data = [[NSMutableData alloc] initWithCapacity:height * width * 4]; 
unsigned char *rawData = data.mutableBytes; 
NSUInteger bytesPerPixel = 4; 
NSUInteger bytesPerRow = bytesPerPixel * width; 
NSUInteger bitsPerComponent = 8; 
CGContextRef context = CGBitmapContextCreate(rawData, width, height, 
              bitsPerComponent, bytesPerRow, colorSpace, 
              kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
CGColorSpaceRelease(colorSpace); 

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 
CGContextRelease(context); 

int byteIndex = (bytesPerRow * 0) + 0 * bytesPerPixel; 

如果你要處理的數據,您通過byteIndex迭代。希望這就是你要找的。

相關問題