2013-10-15 21 views
3

我有以下功能,在iOS 7之前& XCode 5按预期工作。该函数需要一个图像和一个cropSize。该图像是裁剪成指定尺寸的图像,由CGSize cropSize定义。该功能的目的是裁剪图像到一定大小,然后返回裁剪后的图像。在iOS 7上纵向裁剪图像导致方向不正确

- (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之前,所以我不确定发生了什么变化。任何帮助表示感谢,谢谢。

回答

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之前的这些!

+0

镜像方向怎么样? – Andy

+1

太棒了!迄今为止,只有一个也保持清晰的决议 – trdavidson