【问题标题】:Cropping an Image in portrait on iOS 7 results in incorrect orientation在 iOS 7 上纵向裁剪图像会导致方向不正确
【发布时间】: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 之前没有发生过。所以我不确定具体发生了什么变化。任何帮助表示赞赏,谢谢。

【问题讨论】:

    标签: ios objective-c uiimage ios7


    【解决方案1】:

    在这里的答案的帮助下解决了这个问题:https://stackoverflow.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 之前没有这些!

    【讨论】:

    • 镜像方向怎么样?
    • 太棒了!到目前为止,只有一个也保持清晰的分辨率
    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    相关资源
    最近更新 更多