Reputation: 4235
My problem is: UIImage is rotated after processing.
I use a helper class for the image processing called ProcessHelper
. This class has two methods:
+ (unsigned char *) convertUIImageToBitmapRGBA8:(UIImage *) image;
+ (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *)rawData
withWidth:(int) width
withHeight:(int) height;
implementation
+ (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;
}
I
On start I get pixel data:
rawData = [ProcessHelper convertUIImageToBitmapRGBA8:image];
Next I do some processing:
-(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)];
}
After this I returned the workingImage
to parent class and UIImageView
shows it returned but in old size, I mean: image before is WxH and after is WxH but rotated (should be HxW, after rotate). I would like to make the image does not rotate.
This happens when I edit photos from ipad. Screenshots are ok and images from internet like backgrounds are ok.
How can I do this correctly?
Upvotes: 0
Views: 148
Reputation: 4406
use UIGraphicsPushContext(ctx); [image drawInRect:CGRectMake(0, 0, width, height)]; UIGraphicsPopContext();
instead of CGContextDrawImage
. CGContextDrawImage
will flip the image verticaly.
Or Scale and Transform the Context before calling CGContextDrawImage
Upvotes: 1