我試圖創建一個沿X軸,Y軸或兩者翻轉UIImage的方法。我一直在接近,但我的轉化知識不夠好,無法一路走到那裏。這裏是我到目前爲止的代碼:沿任一軸翻轉UIImage
- (UIImage *)flippedImageByAxis:(MVImageFlip)axis{
UIGraphicsBeginImageContext(self.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform verticalFlip = CGAffineTransformMake(1, 0, 0, -1, 0, self.size.height);
CGAffineTransform horizFlip = CGAffineTransformMake(-1.0, 0.0, 0.0, 1.0, self.size.width, 0.0);
if(axis == MVImageFlipXAxis || axis == MVImageFlipXAxisAndYAxis)
CGContextConcatCTM(context, horizFlip);
if(axis == MVImageFlipYAxis || axis == MVImageFlipXAxisAndYAxis)
CGContextConcatCTM(context, verticalFlip);
CGContextDrawImage(context, CGRectMake(0.0, 0.0, self.size.width, self.size.height), [self CGImage]);
UIImage *flipedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return flipedImage;
}
這翻轉圖像,但不正確。 Y翻轉的圖像根本不會翻轉,X翻轉的圖像看起來像XY圖像應該看起來像,並且XY圖像看起來像Y圖像應該看起來像。將變革結合起來讓他們正常工作正在讓我頭痛。
MVImageFlip枚舉只是你在代碼中看到的三個。沒什麼特別的。
我更喜歡圖片在這種情況下的規模是這樣的:'UIGraphicsBeginImageContextWithOptions(self.size,false,self.scale);' –