2013-04-21 94 views
1

我正在開發一個應用程序來裁剪圖像。 我使用下面的代碼來調用裁剪的方法:裁剪圖像讓圖片左旋

UIImage* croppedImage = [self imageCrop:imageView toRect:CGRectMake(10.0, 50.0, 320, 100)]; 

,這裏是該方法的代碼:

{ 
    //create a context to do our clipping in 
    CGRect newRect = CGRectApplyAffineTransform(rect, imageViewToCrop.transform); 
    UIImage *imageToCrop = imageViewToCrop.image; 
    UIGraphicsBeginImageContext(newRect.size); 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    //create a rect with the size we want to crop the image to 
    //the X and Y here are zero so we start at the beginning of our 
    //newly created context 
    CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height); 
    CGContextClipToRect(currentContext, clippedRect); 

    //draw the image to our clipped context using our offset rect 
    CGContextDrawImage(currentContext, newRect, imageToCrop.CGImage); 

    //pull the image from our cropped context 
    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext(); 

    //pop the context to get back to the default 
    UIGraphicsEndImageContext(); 

    return cropped; 
} 

問題是,當我這個圖片設置爲的UIImageView,我發現返回的圖像向左旋轉。我無法將它旋轉起來。 任何人都可以知道這個問題?

回答

1

由於大衛寫的問題很可能是你不保存imageOrientation屬性。你的代碼的問題還在於你不保存比例尺屬性。 嘗試這樣創建裁剪圖像:

CGImageRef croppedRef = CGBitmapContextCreateImage(currentContext); 
UIImage* cropped = [UIImage imageWithCGImage:croppedRef scale:imageToCrop.scale orientation:imageToCrop.imageOrientation]; 
CGImageRelease(croppedRef); 
+0

嗨,我這樣做的時候,我的旋轉仍然關閉。你能想到使用imageOrientation不能正常工作的原因嗎? – Aggressor 2014-09-22 21:43:51

2

Uiimage有一個imageOrientation在某些情況下指示系統應顯示圖像位的方式的屬性。嘗試記錄該值並查看。如果它是我懷疑的那麼你可能需要調整你的裁剪算法,首先創建一個CGImage,然後使用UIImage方法從它製作一個具有方向參數的UIImage。