在 iOS 7 上纵向裁剪图像会导致方向不正确

Posted

技术标签:

【中文标题】在 iOS 7 上纵向裁剪图像会导致方向不正确【英文标题】:Cropping an Image in portrait on iOS 7 results in incorrect orientation 【发布时间】:2013-10-15 20:46:23 【问题描述】:

我有以下功能,在 ios 7 和 XCode 5 之前它按预期工作。该函数采用图像和cropSize。图像是要裁剪为指定大小的图像,该大小由 CGSize cropSize 定义。该函数的目的是将图像裁剪到一定大小,然后返回裁剪后的图像。

    - (UIImage *) cropImage:(UIImage *)originalImage cropSize:(CGSize)cropSize



    //calculate scale factor to go between cropframe and original image
    float SF = originalImage.size.width / cropSize.width;

    //find the centre x,y coordinates of image
    float centreX = originalImage.size.width / 2;
    float centreY = originalImage.size.height / 2;

    //calculate crop parameters
    float cropX = centreX - ((cropSize.width / 2) * SF);
    float cropY = centreY - ((cropSize.height / 2) * SF);

    CGRect cropRect = CGRectMake(cropX, cropY, (cropSize.width *SF), (cropSize.height * SF));

    CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage], cropRect);


    //keep orientation if landscape
    UIImage *newImage;

    if (originalImage.size.width > originalImage.size.height || originalImage.size.width == originalImage.size.height) 
        newImage = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:originalImage.imageOrientation];
    
    else
    
        newImage = [UIImage imageWithCGImage:imageRef];
    

    CGImageRelease(imageRef);

    //Now want to scale down cropped image!
    //want to multiply frames by 2 to get retina resolution
    CGRect scaledImgRect = CGRectMake(0, 0, (cropSize.width * 2), (cropSize.height * 2));

    UIGraphicsBeginImageContextWithOptions(scaledImgRect.size, NO, [UIScreen mainScreen].scale);

    [newImage drawInRect:scaledImgRect];

    UIImage *scaledNewImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return scaledNewImage;


问题在于,它在传入横向的 UIImage 时一切正常,图像按预期裁剪,但是如果传入的图像是纵向拍摄的,那么生成的图像(裁剪后的结果 scaledNewImage) 在其一侧旋转了 90 度,这是我不想要的。

就好像正在处理纵向图像,就好像它是在横向上一样 - 因此该功能裁剪了应该是横向而不是纵向的纵向图像。 如果裁剪区域是方形的,这不是很明显,但是如果要裁剪的区域是横向矩形,那么它会沿着肖像的长度而不是宽度进行裁剪。希望我说得通!

这个问题在 iOS 7 和 XCode 5 之前没有发生过。所以我不确定具体发生了什么变化。任何帮助表示赞赏,谢谢。

【问题讨论】:

【参考方案1】:

在这里的答案的帮助下解决了这个问题:https://***.com/a/14712184/521653

- (UIImage *) cropImage:(UIImage *)originalImage cropSize:(CGSize)cropSize

    NSLog(@"original image orientation:%d",originalImage.imageOrientation);

    //calculate scale factor to go between cropframe and original image
    float SF = originalImage.size.width / cropSize.width;

    //find the centre x,y coordinates of image
    float centreX = originalImage.size.width / 2;
    float centreY = originalImage.size.height / 2;

    //calculate crop parameters
    float cropX = centreX - ((cropSize.width / 2) * SF);
    float cropY = centreY - ((cropSize.height / 2) * SF);

    CGRect cropRect = CGRectMake(cropX, cropY, (cropSize.width *SF), (cropSize.height * SF));

    CGAffineTransform rectTransform;
    switch (originalImage.imageOrientation)
    
        case UIImageOrientationLeft:
            rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(M_PI_2), 0, -originalImage.size.height);
            break;
        case UIImageOrientationRight:
            rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(-M_PI_2), -originalImage.size.width, 0);
            break;
        case UIImageOrientationDown:
            rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(-M_PI), -originalImage.size.width, -originalImage.size.height);
            break;
        default:
            rectTransform = CGAffineTransformIdentity;
    ;
    rectTransform = CGAffineTransformScale(rectTransform, originalImage.scale, originalImage.scale);

    CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectApplyAffineTransform(cropRect, rectTransform));
    UIImage *result = [UIImage imageWithCGImage:imageRef scale:originalImage.scale orientation:originalImage.imageOrientation];
    CGImageRelease(imageRef);
    //return result;

    //Now want to scale down cropped image!
    //want to multiply frames by 2 to get retina resolution
    CGRect scaledImgRect = CGRectMake(0, 0, (cropSize.width * 2), (cropSize.height * 2));

    UIGraphicsBeginImageContextWithOptions(scaledImgRect.size, NO, [UIScreen mainScreen].scale);

    [result drawInRect:scaledImgRect];

    UIImage *scaledNewImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return scaledNewImage;


那是那里的更新方法。我从链接到我的方法的答案中实现了代码,它解决了问题。奇怪我在 iOS 7 之前没有这些!

【讨论】:

镜像方向怎么样? 太棒了!到目前为止,只有一个也保持清晰的分辨率

以上是关于在 iOS 7 上纵向裁剪图像会导致方向不正确的主要内容,如果未能解决你的问题,请参考以下文章

PHP:图像调整大小和裁剪为纵向

图像裁剪编辑器不工作

如何正确裁剪图像以适合 imageview

如何在 xCode 4.2 中为两个方向正确设置 iPad 启动图像

如何从文件中旋转和裁剪 iOS CGImage

iOS - 将图像裁剪为检测到的面部的问题