1

画像をある形式 (.png) から別の画像形式 (.img 形式) に変換したいと考えています。同じ画像フォーマットの rgba 値を取得して変更することができました。他の画像フォーマットに変換するために他に何か必要なことはありますか?

空のビットマップを作成し、このビットマップをイメージするために描画したいと考えています。

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 値で埋める機能がありますか。

4

1 に答える 1

3

次のようになります。

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。それがあなたが探しているものであることを願っています。

于 2013-04-15T11:15:38.557 に答える