objective-c - 我上下颠倒旋转图像,但如何水平翻转

标签 objective-c rotation uiimage flip

我从相机胶卷加载图片,但这张图片是颠倒的。所以我写了旋转它的方法。

CGImageRef imageRef = [image CGImage];

float width = CGImageGetWidth(imageRef);
float height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
Byte *rawData = malloc(height * width * 4);
Byte bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * width;
Byte bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
int byteIndex = 0;
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);

Byte *rawData2 = malloc(height * width * 4);

for (int i = 0 ; i < width * height ; i++) {
    int index = (width * height) * 4;
    rawData2[byteIndex + 0] = rawData[index - byteIndex + 0];
    rawData2[byteIndex + 1] = rawData[index - byteIndex + 1];
    rawData2[byteIndex + 2] = rawData[index - byteIndex + 2];
    rawData2[byteIndex + 3] = rawData[index - byteIndex + 3];

    byteIndex += 4;
}

CGContextRef ctx = CGBitmapContextCreate(rawData2, CGImageGetWidth( imageRef ), CGImageGetHeight( imageRef ), 8, CGImageGetBytesPerRow( imageRef ), CGImageGetColorSpace( imageRef ),kCGImageAlphaPremultipliedLast );

imageRef = CGBitmapContextCreateImage (ctx);
image = [UIImage imageWithCGImage:imageRef];

CGContextRelease(context);

return image;

没关系,但现在我必须水平翻转它,我不知道我该怎么做。我试着在第二天做。

谢谢你的帮助

最佳答案

你试过这个吗:

imageView.transform = CGAffineTransformMakeScale(-1, 1);

?

您还可以使用转换来进行旋转:

imageView.transform = CGAffineTransformMakeRotation(M_PI);

您可以像这样将两个转换合并为一个:

imageView.transform = CGAffineTransformRotation(CGAffineTransformMakeScale(-1, 1), M_PI);

如果你想创建自己的 UIImage 对象,而不是操作 View 和转换,我仍然建议你使用上面描述的方法让 View 绘制你喜欢的图像,然后将您的 UIView 内容转换为 UIImage 对象:

UIGraphicsBeginImageContext(rect.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

关于objective-c - 我上下颠倒旋转图像,但如何水平翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11708429/

相关文章:

iphone - 将 UIImage 设置为 UIButton 的背景

iphone - 设置动画总是出现

iphone - 如何检查用户是否在 iOS 设备上启用了 PIN 码

ios - 为什么 self.view.frame.size.width 在 iPad 横向模式下显示为 768

ios - 将 PFFile 转换为 UIImage

ios - 如何让UIImage变圆

iphone - UISearchBar:单击时如何停止调整大小?

java - 旋转过渡后如何将节点返回到其原始位置

c# - 在 C# 中将 2D 图像旋转到一个点

iOS App 轮换导致卡顿