2015-06-09 155 views
0

我想從文件(文件系統上的一個jpeg)旋轉一個計算出來的弧度量的UIImage,但我想圍繞圖像中的一個點旋轉它,以及保持圖像的原始尺寸(圖像中不存在圖像數據不再存在的透明間隙,以及已移出原始幀的裁剪圖像數據)。然後我想存儲並顯示生成的UIImage。我還沒有找到這項任務的任何資源,任何幫助將不勝感激!圍繞一個點旋轉CIImage

到目前爲止,我已經找到(稍作修改)最接近的是如下:

- (的UIImage *)rotateImage:(的UIImage *)圖像aroundPoint:(CGPoint)點弧度:(浮點)弧度newSize:(CGRect)newSize { CGRect imageRect = {point,image.size};

UIGraphicsBeginImageContext(image.size); 

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(context, imageRect.origin.x, imageRect.origin.y); 
CGContextRotateCTM(context, radians); 
CGContextTranslateCTM(context, -imageRect.origin.x, -imageRect.origin.y); 
CGContextDrawImage(context, (CGRect){ CGPointZero, imageRect.size }, [image CGImage]); 
UIImage *returnImg = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 
return returnImg; 

}

不幸的是,這種不正確地旋轉圖像(在我的測試,在180度以上所期望的附近某處)。

+0

添加我當前的代碼,導致圖像被旋轉180度多於所需的。 – eurekabeacon

+0

另外,如果我將旋轉設置爲: CGContextRotateCTM(context,radians + M_PI); 它正確旋轉,但向右移動。 – eurekabeacon

回答

1

旋轉該UIImage的形象,可以說90度,你可以很容易做到:

imageView.transform = CGAffineTransformMakeRotation(M_PI/2); 

多次轉動它,你可以使用:

UIView animateKeyframesWithDuration method 

,並把它錨定到某個點您可以使用:

[image.layer setAnchorPoint:CGPointMake(....)]; 
+1

我不想更改/操作UIImageView,我試圖更改實際的底層UIImage。 – eurekabeacon