【问题标题】:I rotate image upside down but how to flip it horizontally我将图像倒置但如何水平翻转
【发布时间】:2012-07-27 08:23:16
【问题描述】:

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

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;

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

谢谢你的帮助

【问题讨论】:

    标签: objective-c rotation uiimage flip


    【解决方案1】:

    你试过了吗:

    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();
    

    【讨论】:

    • 我必须转换 UIImage 并在 CGContextRef 上绘制它,所以 .transform 不可用。我尝试将 UIImage 添加到 UIImageView,旋转它并制作 image = imageView.image,但它不起作用。
    • 请参阅我的编辑,了解如何将视图内容“转换”为图像。
    • 它正在工作,但我设置了imageview.frame = CGRectMake(111,122,768,1024),它在0,0,768,1024 呈现上下文。有什么建议吗?
    • 好的,谢谢您的帮助。我将 imageView 添加到 subView 并渲染它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2011-06-09
    相关资源
    最近更新 更多