私の問題は、処理後に UIImage が回転することです。
という画像処理用のヘルパー クラスを使用しますProcessHelper
。このクラスには 2 つのメソッドがあります。
+ (unsigned char *) convertUIImageToBitmapRGBA8:(UIImage *) image;
+ (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *)rawData
withWidth:(int) width
withHeight:(int) height;
実装
+ (unsigned char *) convertUIImageToBitmapRGBA8:(UIImage *) image {
NSLog(@"Convert image [%d x %d] to RGBA8 char data", (int)image.size.width,
(int)image.size.height);
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
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);
return rawData;
}
+ (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *) rawData
withWidth:(int) width
withHeight:(int) height {
CGContextRef ctx = CGBitmapContextCreate(rawData,
width,
height,
8,
width * 4,
CGColorSpaceCreateDeviceRGB(),
kCGImageAlphaPremultipliedLast );
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
free(rawData);
return rawImage;
}
私
開始時にピクセル データを取得します。
rawData = [ProcessHelper convertUIImageToBitmapRGBA8:image];
次に、いくつかの処理を行います。
-(void)process_grayscale {
int byteIndex = 0;
for (int i = 0 ; i < workingImage.size.width * workingImage.size.height ; ++i)
{
int outputColor = (rawData[byteIndex] + rawData[byteIndex+1] + rawData[byteIndex+2]) / 3;
rawData[byteIndex] = rawData[byteIndex + 1] = rawData[byteIndex + 2] = (char) (outputColor);
byteIndex += 4;
}
workingImage = [ProcessHelper convertBitmapRGBA8ToUIImage:rawData
withWidth:CGImageGetWidth(workingImage.CGImage)
withHeight:CGImageGetHeight(workingImage.CGImage)];
}
この後、workingImage
親クラスに戻り、UIImageView
返されたが古いサイズで表示されます。つまり、前の画像は WxH で、後の画像は WxH ですが回転しています (回転後は HxW である必要があります)。画像が回転しないようにしたいです。
これは、iPad から写真を編集するときに発生します。スクリーンショットは問題ありませんし、背景のようなインターネットからの画像も問題ありません。
どうすればこれを正しく行うことができますか?