4

我有以下功能,在 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 之前没有发生过。所以我不确定到底发生了什么变化。任何帮助表示赞赏,谢谢。

4

1 回答 1

16

借助此处的答案解决了此问题: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 之前没有这些!

于 2013-10-19T10:42:22.047 回答