Ter*_*rry 17 iphone core-graphics uiimagepickercontroller uiimage
我想弄清楚是否有任何方法来镜像图像.例如,拍摄某人脸部的照片,然后将其切成两半,并显示每个镜像的脸部样子.在CGAffineTransform函数中似乎没有这样的技巧.图形专家请帮忙!!!
war*_*enm 39
这里的基本"技巧"是使用关于X或Y轴的缩放变换,因子为-1.例如,您可以使用它来创建"绕水平轴翻转"变换:
CGAffineTransform transform = CGAffineTransformScale(transform, -1, 1);
Run Code Online (Sandbox Code Playgroud)
然后,您可以transform在a上设置属性UIImageView以翻转指定的图像,或者将其与另一个转换连接以执行更复杂的效果.为了获得您描述的确切效果,您可能需要编写一些自定义绘图代码以将原始图像绘制到上下文中,然后将翻转的一半覆盖在其上面.这在Core Graphics中相对简单.
tc.*_*tc. 19
如果您只打算支持4.0+
UIImageOrientation flippedOrientation = UIImageOrientationUpMirrored;
switch (image.imageOrientation) {
case UIImageOrientationUp: break;
case UIImageOrientationDown: flippedOrientation = UIImageOrientationDownMirrored; break;
// ...
}
UIImage * flippedImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:flippedOrientation];
Run Code Online (Sandbox Code Playgroud)
sab*_*aba 12
你可能会想,为什么要烦恼这个令人发指的长切换声明呢?
? UIImage *flip = [UIImage imageWithCGImage:image.CGImage
? scale:image.scale
? orientation:(image.imageOrientation + 4) % 8];
Run Code Online (Sandbox Code Playgroud)
如果你看一下枚举,你可以看到模运算会做:
typedef NS_ENUM(NSInteger, UIImageOrientation) {
UIImageOrientationUp, // default orientation
UIImageOrientationDown, // 180 deg rotation
UIImageOrientationLeft, // 90 deg CCW
UIImageOrientationRight, // 90 deg CW
UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip
UIImageOrientationDownMirrored, // horizontal flip
UIImageOrientationLeftMirrored, // vertical flip
UIImageOrientationRightMirrored, // vertical flip
};
Run Code Online (Sandbox Code Playgroud)
但是这段代码太聪明了.您应该编写一个带有显式switch语句的函数.例如
UIImageOrientation mirroredImageOrientation(UIImageOrientation orientation) {
switch(orientation) {
case UIImageOrientationUp: return UIImageOrientationUpMirrored;
case UIImageOrientationDown: return UIImageOrientationDownMirrored;
case UIImageOrientationLeft: return UIImageOrientationLeftMirrored;
case UIImageOrientationRight: return UIImageOrientationRightMirrored;
case UIImageOrientationUpMirrored: return UIImageOrientationUp;
case UIImageOrientationDownMirrored: return UIImageOrientationDown;
case UIImageOrientationLeftMirrored: return UIImageOrientationLeft;
case UIImageOrientationRightMirrored: return UIImageOrientationRight;
default: return orientation;
}
}
Run Code Online (Sandbox Code Playgroud)
并使用这样的功能:
UIImage *flip = [UIImage imageWithCGImage:image.CGImage
scale:image.scale
orientation:mirroredImageOrientation(image.imageOrientation)];
Run Code Online (Sandbox Code Playgroud)
我添加了问号来表示可疑的,有臭味的代码.类似于编程实践
上面的答案都没有回应问题的部分,这部分镜像反映了图像的一半而没有翻转整个图像.混合解决方案会产生以下示例函数,您可以将其用作UIImage + Mirroring等类别:
(UIImage *) horizontalMirror {
UIImageOrientation flippedOrientation = UIImageOrientationUpMirrored;
switch (self.imageOrientation) {
case UIImageOrientationUp: break;
case UIImageOrientationDown: flippedOrientation = UIImageOrientationDownMirrored; break;
}
UIImage * flippedImage = [UIImage imageWithCGImage:self.CGImage scale:1.0 orientation:flippedOrientation];
CGImageRef inImage = self.CGImage;
CGContextRef ctx = CGBitmapContextCreate(NULL,
CGImageGetWidth(inImage),
CGImageGetHeight(inImage),
CGImageGetBitsPerComponent(inImage),
CGImageGetBytesPerRow(inImage),
CGImageGetColorSpace(inImage),
CGImageGetBitmapInfo(inImage)
);
CGRect cropRect = CGRectMake(flippedImage.size.width/2, 0, flippedImage.size.width/2, flippedImage.size.height);
CGImageRef TheOtherHalf = CGImageCreateWithImageInRect(flippedImage.CGImage, cropRect);
CGContextDrawImage(ctx, CGRectMake(0, 0, CGImageGetWidth(inImage), CGImageGetHeight(inImage)), inImage);
CGAffineTransform transform = CGAffineTransformMakeTranslation(flippedImage.size.width, 0.0);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
CGContextConcatCTM(ctx, transform);
CGContextDrawImage(ctx, cropRect, TheOtherHalf);
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return finalImage;
}
Run Code Online (Sandbox Code Playgroud)
使用更简单:
UIImage(assetIdentifier: .myIcon)?.withHorizontallyFlippedOrientation()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23311 次 |
| 最近记录: |