<small id='acuqf'></small><noframes id='acuqf'>

      <legend id='acuqf'><style id='acuqf'><dir id='acuqf'><q id='acuqf'></q></dir></style></legend>
      <tfoot id='acuqf'></tfoot>

      1. <i id='acuqf'><tr id='acuqf'><dt id='acuqf'><q id='acuqf'><span id='acuqf'><b id='acuqf'><form id='acuqf'><ins id='acuqf'></ins><ul id='acuqf'></ul><sub id='acuqf'></sub></form><legend id='acuqf'></legend><bdo id='acuqf'><pre id='acuqf'><center id='acuqf'></center></pre></bdo></b><th id='acuqf'></th></span></q></dt></tr></i><div id='acuqf'><tfoot id='acuqf'></tfoot><dl id='acuqf'><fieldset id='acuqf'></fieldset></dl></div>
          <bdo id='acuqf'></bdo><ul id='acuqf'></ul>

        如何将 UIImage 和图像数据旋转 90 度

        How Can I Rotate A UIImage and Image Data By 90 Degrees(如何将 UIImage 和图像数据旋转 90 度)
        <tfoot id='1LmUI'></tfoot>

        <legend id='1LmUI'><style id='1LmUI'><dir id='1LmUI'><q id='1LmUI'></q></dir></style></legend>

          • <bdo id='1LmUI'></bdo><ul id='1LmUI'></ul>

            <small id='1LmUI'></small><noframes id='1LmUI'>

          • <i id='1LmUI'><tr id='1LmUI'><dt id='1LmUI'><q id='1LmUI'><span id='1LmUI'><b id='1LmUI'><form id='1LmUI'><ins id='1LmUI'></ins><ul id='1LmUI'></ul><sub id='1LmUI'></sub></form><legend id='1LmUI'></legend><bdo id='1LmUI'><pre id='1LmUI'><center id='1LmUI'></center></pre></bdo></b><th id='1LmUI'></th></span></q></dt></tr></i><div id='1LmUI'><tfoot id='1LmUI'></tfoot><dl id='1LmUI'><fieldset id='1LmUI'></fieldset></dl></div>

                  <tbody id='1LmUI'></tbody>
                • 本文介绍了如何将 UIImage 和图像数据旋转 90 度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个包含图像的 UIImageView.此时,用户可以单击将 UIImageView 中的图像保存到磁盘.

                  I have a UIImageView that contains an image. At the minute the user can click to save the image within the UIImageView to disk.

                  我想让用户可以点击旋转 UIImageView 并在视图中旋转 UImage 以便在保存图像时保持新的旋转.

                  I would like to make it so that the the user can click to rotate the UIImageView and also rotate the UImage within the view so that when the image is saved it keeps the new rotation.

                  此刻我有

                  - (IBAction)rotateImage:(id)sender {
                  
                  float degrees = 90; //the value in degrees
                  self.imagePreview.transform = CGAffineTransformMakeRotation(degrees * M_PI/180);
                  
                  }
                  

                  有人可以指出我保持当前旋转的正确方向吗?

                  Can someone point me in the right direction regarding keeping the current rotation.

                  谢谢

                  推荐答案

                  我的旧应用程序中有这样的代码.但是这段代码可以简化,因为您无需将其旋转到非 90 度角.

                  I have such code in my old app. But this code can be simplified because you have no need to rotate it on non-90 degrees angle.

                  Objective-C

                  @implementation UIImage (Rotation)
                  
                  - (UIImage *)imageRotatedOnDegrees:(CGFloat)degrees
                  {
                    // Follow ing code can only rotate images on 90, 180, 270.. degrees.
                    CGFloat roundedDegrees = (CGFloat)(round(degrees / 90.0) * 90.0);
                    BOOL sameOrientationType = ((NSInteger)roundedDegrees) % 180 == 0;
                    CGFloat radians = M_PI * roundedDegrees / 180.0;
                    CGSize newSize = sameOrientationType ? self.size : CGSizeMake(self.size.height, self.size.width);
                  
                    UIGraphicsBeginImageContext(newSize);
                    CGContextRef ctx = UIGraphicsGetCurrentContext();
                    CGImageRef cgImage = self.CGImage;
                    if (ctx == NULL || cgImage == NULL) {
                      UIGraphicsEndImageContext();
                      return self;
                    }
                  
                    CGContextTranslateCTM(ctx, newSize.width / 2.0, newSize.height / 2.0);
                    CGContextRotateCTM(ctx, radians);
                    CGContextScaleCTM(ctx, 1, -1);
                    CGPoint origin = CGPointMake(-(self.size.width / 2.0), -(self.size.height / 2.0));
                    CGRect rect = CGRectZero;
                    rect.origin = origin;
                    rect.size = self.size;
                    CGContextDrawImage(ctx, rect, cgImage);
                    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
                    UIGraphicsEndImageContext();
                    return image ?: self;
                  }
                  
                  @end
                  

                  斯威夫特

                  extension UIImage {
                  
                    func imageRotated(on degrees: CGFloat) -> UIImage {
                      // Following code can only rotate images on 90, 180, 270.. degrees.
                      let degrees = round(degrees / 90) * 90
                      let sameOrientationType = Int(degrees) % 180 == 0
                      let radians = CGFloat.pi * degrees / CGFloat(180)
                      let newSize = sameOrientationType ? size : CGSize(width: size.height, height: size.width)
                  
                      UIGraphicsBeginImageContext(newSize)
                      defer {
                        UIGraphicsEndImageContext()
                      }
                  
                      guard let ctx = UIGraphicsGetCurrentContext(), let cgImage = cgImage else {
                        return self
                      }
                  
                      ctx.translateBy(x: newSize.width / 2, y: newSize.height / 2)
                      ctx.rotate(by: radians)
                      ctx.scaleBy(x: 1, y: -1)
                      let origin = CGPoint(x: -(size.width / 2), y: -(size.height / 2))
                      let rect = CGRect(origin: origin, size: size)
                      ctx.draw(cgImage, in: rect)
                      let image = UIGraphicsGetImageFromCurrentImageContext()
                      return image ?? self
                    }
                  
                  }
                  

                  这篇关于如何将 UIImage 和图像数据旋转 90 度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  what#39;s property copy means in Cocoa#39;s Framework?(like UITabBar#39;s items property)(Cocoa 框架中的属性副本是什么意思?(如 UITabBar 的 items 属性))
                  Can`t copy file from bundle to documents directory in iOS(无法将文件从捆绑包复制到 iOS 中的文档目录)
                  Automatically copy property values from one object to another of a different type but the same protocol (Objective-C)(自动将属性值从一个对象复制到另一个类型不同但协议相同的对象 (Objective-C))
                  Copy text to clipboard with iOS(使用 iOS 将文本复制到剪贴板)
                  How to copy text to clipboard/pasteboard with Swift(如何使用 Swift 将文本复制到剪贴板/粘贴板)
                  How to show the progress of copying a large file in iOS?(如何在iOS中显示复制大文件的进度?)

                    <tbody id='Z8irf'></tbody>

                    1. <legend id='Z8irf'><style id='Z8irf'><dir id='Z8irf'><q id='Z8irf'></q></dir></style></legend>

                      <small id='Z8irf'></small><noframes id='Z8irf'>

                        <bdo id='Z8irf'></bdo><ul id='Z8irf'></ul>
                        • <tfoot id='Z8irf'></tfoot>
                          <i id='Z8irf'><tr id='Z8irf'><dt id='Z8irf'><q id='Z8irf'><span id='Z8irf'><b id='Z8irf'><form id='Z8irf'><ins id='Z8irf'></ins><ul id='Z8irf'></ul><sub id='Z8irf'></sub></form><legend id='Z8irf'></legend><bdo id='Z8irf'><pre id='Z8irf'><center id='Z8irf'></center></pre></bdo></b><th id='Z8irf'></th></span></q></dt></tr></i><div id='Z8irf'><tfoot id='Z8irf'></tfoot><dl id='Z8irf'><fieldset id='Z8irf'></fieldset></dl></div>