为什么调整图像大小的代码会将其与原始图像旋转90度?

时间:2013-04-23 18:21:31

标签: ios objective-c uiimage

我有一个iPad应用程序,我正在使用相机。原始图像是480 x 640.我正在尝试将其大小调整为124 x 160,然后使用我在互联网上找到的代码将其存储在CoreData中:

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {

CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGImageRef imageRef = image.CGImage;

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

CGContextConcatCTM(context, flipVertical);

// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef);

// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

CGImageRelease(newImageRef);
UIGraphicsEndImageContext();

return newImage;

}

将图像逆时针旋转90度返回给我,我不明白为什么。我试着评论这句话:

CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

但没有区别。这有什么不对?

2 个答案:

答案 0 :(得分:0)

感谢所有提出建议的人。我一直在寻找并找到了这个,它可以扩展并保持方向:

    CGRect screenRect = CGRectMake(0, 0, 120.0, 160.0);
    UIGraphicsBeginImageContext(screenRect.size);
    [image drawInRect:screenRect blendMode:kCGBlendModePlusDarker alpha:1];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

答案 1 :(得分:-1)

在绘制图像时,您需要考虑图像的方向。请检查此回答iOS - UIImageView - how to handle UIImage image orientation