Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 图像在处理后旋转。如何才能正确地执行此操作?_Objective C_Uiimage_Quartz Graphics - Fatal编程技术网

Objective c 图像在处理后旋转。如何才能正确地执行此操作?

Objective c 图像在处理后旋转。如何才能正确地执行此操作?,objective-c,uiimage,quartz-graphics,Objective C,Uiimage,Quartz Graphics,我的问题是:UIImage在处理后会旋转 我使用一个名为ProcessHelper的助手类进行图像处理。此类有两种方法: + (unsigned char *) convertUIImageToBitmapRGBA8:(UIImage *) image; + (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *)rawData withWidth:(int) width

我的问题是: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)];
}
-(无效)处理\u灰度{
int byteIndex=0;
对于(int i=0;i
在此之后,我将
workingImage
返回到父类,并且
UIImageView
显示返回的图像,但大小较旧,我的意思是:前面的图像是WxH,后面的图像是WxH,但旋转(应该是HxW,旋转后)。我想使图像不旋转

当我从ipad编辑照片时就会发生这种情况。屏幕截图还可以,来自类似互联网背景的图像也可以


如何正确执行此操作?

使用
UIGraphicsPushContext(ctx);[image drawInRect:CGRectMake(0,0,宽度,高度)];UIGraphicsPopContext()而不是
CGContextDrawImage
CGContextDrawImage
将垂直翻转图像

或者在调用
CGContextDrawImage

之前缩放和转换上下文(谢谢您的帮助;)现在每个图像都颠倒了,我不知道为什么。现在我可以翻转它了,一切正常,但我不知道为什么图像并没有原始旋转。