处理后图像旋转。我怎么能正确地做到这一点?

时间:2012-10-18 12:43:19

标签: objective-c uiimage quartz-graphics

我的问题是:处理后旋转UIImage。

我使用一个名为ProcessHelper的图像处理辅助类。这个类有两种方法:

+ (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编辑照片时会发生这种情况。屏幕截图是可以的,来自互联网的图像就像背景一样。

我该如何正确地做到这一点?

1 个答案:

答案 0 :(得分:1)

使用UIGraphicsPushContext(ctx); [image drawInRect:CGRectMake(0, 0, width, height)]; UIGraphicsPopContext();代替CGContextDrawImageCGContextDrawImage将垂直翻转图像。

或在调用CGContextDrawImage

之前缩放和转换上下文