0

我有以下代码:

    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 时,纵向模式可以正常工作。但是横向模式有问题。

我试图通过裁剪照片的左右边缘来使风景与我的肖像大小相同。

还注意到如果我在拍照时翻转风景相机,方向会使我的照片倒置。

我这样做是错误的吗?

谢谢。

4

1 回答 1

0

尝试改变

[UIImage imageWithCGImage: imageRef];

[UIImage imageWithCGImage:imageRef scale: photo.scale orientation:UIImageOrientationUp];

或者

[UIImage imageWithCGImage:imageRef scale: photo.scale orientation:photo.imageOrientation];
于 2014-07-14T07:42:32.190 回答