2013-10-15 20 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