我有以下代码:
float photoWidth = photo.size.width;
float photoHeight = photo.size.height;
UIImage *picture = nil;
if (photoWidth < photoHeight)
{
// Portrait
// Scale the photo down
CGRect rect = CGRectMake(0.0, 0.0, 450.0, 600.0);
UIGraphicsBeginImageContext( rect.size );
[photo drawInRect:rect];
picture = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
else
{
// Landscape
// Crop off the edges
float scale = photoHeight / photoWidth;
float newWidth = photoHeight * scale;
float newHeight = photoHeight;
float newX = (newHeight - newWidth) / 2.0;
float newY = 0.0;
CGRect cropped = CGRectMake(newX, newY, newWidth, newHeight);
CGImageRef imageRef = CGImageCreateWithImageInRect ([photo CGImage], cropped);
UIImage * croppedPhoto = [UIImage imageWithCGImage: imageRef];
CGImageRelease (imageRef);
// Scale the photo down
CGRect rect = CGRectMake(0.0, 0.0, 450.0, 600.0);
UIGraphicsBeginImageContext( rect.size );
[croppedPhoto drawInRect:rect];
picture = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
NSData *photoData = UIImagePNGRepresentation(picture);
当我使用photoData并将其转换为UIImage时,肖像模式工作正常。但是横向模式存在问题。
我试图通过裁剪照片的左右边缘使景观与我的肖像大小相同。
还注意到,如果我在拍照时翻转了风景照相机,方向会使我的照片上下颠倒。
我这样做是错误的吗?
感谢。
答案 0 :(得分:0)
尝试更改
[UIImage imageWithCGImage: imageRef];
到
[UIImage imageWithCGImage:imageRef scale: photo.scale orientation:UIImageOrientationUp];
或
[UIImage imageWithCGImage:imageRef scale: photo.scale orientation:photo.imageOrientation];