pra*_*epa 109 uiimage ios uiimageorientation
如何UIImage水平翻转,我UIImageOrientationUpMirrored在UIImage类引用中发现了枚举值,如何利用这个属性来翻转UIImage.
aro*_*oth 230
UIImage* sourceImage = [UIImage imageNamed:@"whatever.png"];
UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage
scale:sourceImage.scale
orientation:UIImageOrientationUpMirrored];
Run Code Online (Sandbox Code Playgroud)
A. *_*dam 69
一个非常简单的方法是通过创建UIImageView而不是UIImage并在UIImageView上进行转换.
yourImageView.image =[UIImage imageNamed:@"whatever.png"];
yourImageView.transform = CGAffineTransform(scaleX: -1, y: 1); //Flipped
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
Ale*_*sov 35
垂直翻转通常需要使用初始化OpenGL纹理glTexImage2d(...).上面提出的技巧实际上并没有修改图像数据,在这种情况下不起作用.这是一个代码,用于实现/sf/answers/1253656071/启发的实际数据翻转
- (UIImage *)flipImage:(UIImage *)image
{
UIGraphicsBeginImageContext(image.size);
CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(0.,0., image.size.width, image.size.height),image.CGImage);
UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return i;
}
Run Code Online (Sandbox Code Playgroud)
aba*_*net 13
我尝试过使用imageFlippedForRightToLeftLayoutDirection,并创建一个具有不同方向的新UIImage,但至少这是我找到的翻转图像的唯一解决方案
let ciimage: CIImage = CIImage(CGImage: imagenInicial.CGImage!)
let rotada3 = ciimage.imageByApplyingTransform(CGAffineTransformMakeScale(-1, 1))
Run Code Online (Sandbox Code Playgroud)
当然,让finalImage = UIImage(CIImage:rotada3)
Hen*_*nry 12
因为它的图像方向定义:
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)
我在更多情况下做了一些改进,例如从AVCaptureSession处理UIImage.
UIImage* sourceImage = [UIImage imageNamed:@"whatever.png"];
UIImageOrientation flipingOrientation;
if(sourceImage.imageOrientation>=4){
flippedOrientation = sourceImage.imageOrientation - 4;
}else{
flippedOrientation = sourceImage.imageOrientation + 4;
}
UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage
scale: sourceImage.scale orientation: flipingOrientation];
Run Code Online (Sandbox Code Playgroud)
小智 9
这里的版本很快:(我在评论中看到了这个问题)
let srcImage = UIImage(named: "imageName")
let flippedImage = UIImage(CGImage: srcImage.CGImage, scale: srcImage.scale, orientation: UIImageOrientation.UpMirrored)
Run Code Online (Sandbox Code Playgroud)
这是一个水平镜像/翻转UIImage的可靠实现,可以来回应用于图像.由于它会更改底层图像数据,因此绘图(如屏幕截图)也会发生变化.经过测试,没有质量损失.
func flipImage() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let bitmap = UIGraphicsGetCurrentContext()!
bitmap.translateBy(x: size.width / 2, y: size.height / 2)
bitmap.scaleBy(x: -1.0, y: -1.0)
bitmap.translateBy(x: -size.width / 2, y: -size.height / 2)
bitmap.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image?
}
Run Code Online (Sandbox Code Playgroud)
可能这对某些人有用:
UIImageOrientation imageOrientation;
switch (sourceImage.imageOrientation) {
case UIImageOrientationDown:
imageOrientation = UIImageOrientationDownMirrored;
break;
case UIImageOrientationDownMirrored:
imageOrientation = UIImageOrientationDown;
break;
case UIImageOrientationLeft:
imageOrientation = UIImageOrientationLeftMirrored;
break;
case UIImageOrientationLeftMirrored:
imageOrientation = UIImageOrientationLeft;
break;
case UIImageOrientationRight:
imageOrientation = UIImageOrientationRightMirrored;
break;
case UIImageOrientationRightMirrored:
imageOrientation = UIImageOrientationRight;
break;
case UIImageOrientationUp:
imageOrientation = UIImageOrientationUpMirrored;
break;
case UIImageOrientationUpMirrored:
imageOrientation = UIImageOrientationUp;
break;
default:
break;
}
resultImage = [UIImage imageWithCGImage:sourceImage.CGImage scale:sourceImage.scale orientation:imageOrientation];
Run Code Online (Sandbox Code Playgroud)
对于Swift 3/4:
imageView.transform = CGAffineTransform(scaleX: -1, y: 1)
Run Code Online (Sandbox Code Playgroud)
iOS 10以上
[myImage imageWithHorizontallyFlippedOrientation];
Run Code Online (Sandbox Code Playgroud)
斯威夫特4:
let flippedImage = myImage.withHorizontallyFlippedOrientation()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
66204 次 |
| 最近记录: |