-2

我有一个使用相机的 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);

但这没有什么区别。这里有什么问题?

4

2 回答 2

0

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

    CGRect screenRect = CGRectMake(0, 0, 120.0, 160.0);
    UIGraphicsBeginImageContext(screenRect.size);
    [image drawInRect:screenRect blendMode:kCGBlendModePlusDarker alpha:1];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
于 2013-04-23T20:18:09.880 回答
-1

像这样绘制图像时需要考虑图像的方向。检查这个答案iOS - UIImageView - 如何处理 UIImage 图像方向

于 2013-04-23T18:28:31.807 回答