【问题标题】:IOS Objective C Keep Orienation but Rotate image DataIOS Objective C保持方向但旋转图像数据
【发布时间】:2016-04-18 18:30:32
【问题描述】:

真的希望你能帮我解决这个困扰我一整天的问题

是否可以保持 UIImage 的方向(比如横向)但旋转里面的图像?

我使用以下代码保存我的 UIImage

[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];

其中图像是 UIImage。

下图显示了根据设备方向保存的内容。

底部 4 是我想要实现的目标

基本上,无论您以哪种方式转动设备,我都试图让图片在横向模式下处于其一侧

感谢任何帮助

标记

【问题讨论】:

    标签: objective-c rotation uiimage orientation


    【解决方案1】:

    试试这个

    UIImage* landscapeImage = [UIImage imageWithCGImage:image.CGImage
                                                scale:image.scale
                                          orientation:UIImageOrientationLeft];
    [UIImagePNGRepresentation(landscapeImage) writeToFile:pngPath atomically:YES];
    

    【讨论】:

    • 抱歉,它什么也没做>
    • 以上代码仅将任何方向更改为横向左侧。如果您正在谈论旋转图像,请参考链接stackoverflow.com/questions/11667565/…
    • 谢谢,我看到一个长约 100 个,全部将图像旋转到我想要的位置,但要么同时旋转方向,要么压缩图像以适应
    • 我想我可能可以在您提供的链接上修改一个名为 RotateImage 的链接,该链接获取图像和旋转只需要帮助解决如何裁剪图像以适应横向
    • 我认为这两行是关键 UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)]; CGContextDrawImage(位图,CGRectMake(-image.size.width,-image.size.height,image.size.width,image.size.height),[图像CGImage]);我认为第一个设置要从原始图像中抓取的区域,第二行将其绘制到新图像,但不知道该怎么做
    【解决方案2】:

    我会正常保存图像文件,然后在 UI 中更改 imageView。只要图像始终处于纵向模式,您就可以将图像视图旋转 90 度,如下所示:

    self.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    

    然后将 imageView 模式设置为 Aspect Fit

    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    

    但是,如果您的图像需要以其他方式旋转或者如果它已经位于横向位置,这将不起作用。

    【讨论】:

      【解决方案3】:

      对固定拍摄图像的方向有帮助吗?

      https://stackoverflow.com/a/15039536

      如果没有,也许您可​​以为自己的问题找到灵感。基本上创建具有正确宽度/高度的上下文,旋转变换,绘制图像,存储为新图像。

      CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
          CGImageGetBitsPerComponent(image.CGImage), 0,
          CGImageGetColorSpace(image.CGImage),
          CGImageGetBitmapInfo(image.CGImage));
          CGContextConcatCTM(ctx, transform);
      CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
      //or
      CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);
      
      CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
      UIImage *img = [UIImage imageWithCGImage:cgimg];
      

      还可以阅读有关相机和图库的不同结果的 cmets。

      如果您只想显示它旋转,您可以像@bryannorden 建议的那样转换 UIView。

      【讨论】:

      • 已经嵌入了你的图像,现在我看到了,你总是希望这个人是纵向的,无论它是在什么方向上拍摄的。所以你需要在旋转之前(或旋转之后)裁剪横向图像)。您还可以做的是在我的示例中更改 CGRectMake,从负坐标开始并使其绘制在画布之外。
      • 如果您使用 ImageView,则需要剪辑,请参见此处:stackoverflow.com/questions/6638623/…
      【解决方案4】:
      NSBitmapImageRep *bit_map = [[NSBitmapImageRep alloc]
                                   initWithBitmapDataPlanes:NULL
                                   pixelsWide:img_size.width
                                   pixelsHigh:img_size.height
                                   bitsPerSample:8
                                   samplesPerPixel:4
                                   hasAlpha:YES
                                   isPlanar:NO
                                   colorSpaceName:NSDeviceRGBColorSpace
                                   bitmapFormat:NSAlphaFirstBitmapFormat
                                   bytesPerRow:0
                                   bitsPerPixel:0];
      NSAffineTransform *trans2=[NSAffineTransform transform];
      [trans2 translateXBy:dirtyRect.size.width/2 yBy:dirtyRect.size.height/2];
      [trans2 rotateByDegrees: angle];
      [trans2 concat];
      
      NSGraphicsContext *g=[NSGraphicsContext graphicsContextWithBitmapImageRep:bit_map];
      [NSGraphicsContext saveGraphicsState];
      [NSGraphicsContext setCurrentContext:g];
      [img drawInRect:NSMakeRect(0, 0, img_size.width, img_size.height)];
      [NSGraphicsContext restoreGraphicsState];
      
      
      [bit_map drawInRect:NSMakeRect(-img_size.width/2, -img_size.height/2, img_size.width, img_size.height)];
      

      查看实际操作:https://www.youtube.com/watch?v=DD_KYaq3SSg

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多