我想從文件(文件系統上的一個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度以上所期望的附近某處)。
添加我當前的代碼,導致圖像被旋轉180度多於所需的。 – eurekabeacon
另外,如果我將旋轉設置爲: CGContextRotateCTM(context,radians + M_PI); 它正確旋轉,但向右移動。 – eurekabeacon