微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我从相机胶卷加载图像,但此图像是颠倒的.所以我写了旋转它的方法.
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,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对象,而不是操纵视图和转换,我仍然建议你使用上面描述的方法使视图根据你的意愿绘制图像,然后将你的UIView内容转换为UIImage对象:

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

原文地址:https://www.jb51.cc/c/117619.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐