在保存为PNG之前将新创建的iOS图像旋转90度

 社会逼我爱上Money_717 发布于 2023-02-08 12:10

我已经阅读了许多与此相关的答案,但我仍然无法让它发挥作用.

我有一个用户可以在其中签名的视图.以下是它的外观:http://d.pr/i/McuE

我可以成功检索此图像并将其保存到文件系统,但我需要在保存前将其旋转90度,以便签名从左到右读取.

// Grab the image
UIGraphicsBeginImageContext(self.signArea.bounds.size);
[self.signArea drawRect: self.signArea.bounds];
UIImage *signatureImage = UIGraphicsGetImageFromCurrentImageContext();

//-- ? -- Rotate the image (this doesn't work) -- ? --
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRotateCTM(context, M_PI_2);

UIGraphicsEndImageContext();

//Save the image as a PNG in Documents folder
[UIImagePNGRepresentation(signatureImage) writeToFile:[[PPHelpers documentsPath] stringByAppendingPathComponent:@"signature-temp.png"] atomically:YES];

如何在保存之前旋转图像?

在此先感谢您的帮助.我使用iOS 7 SDK在Xcode 5上.

1 个回答
  • 我终于使用这篇文章中的一个答案来解决这个问题:如何将UIImage旋转90度?

    我用这个方法:

    - (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees{
      //Calculate the size of the rotated view's containing box for our drawing space
      UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,oldImage.size.width, oldImage.size.height)];
      CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI / 180);
      rotatedViewBox.transform = t;
      CGSize rotatedSize = rotatedViewBox.frame.size;
    
      //Create the bitmap context
      UIGraphicsBeginImageContext(rotatedSize);
      CGContextRef bitmap = UIGraphicsGetCurrentContext();
    
      //Move the origin to the middle of the image so we will rotate and scale around the center.
      CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
    
      //Rotate the image context
      CGContextRotateCTM(bitmap, (degrees * M_PI / 180));
    
      //Now, draw the rotated/scaled image into the context
      CGContextScaleCTM(bitmap, 1.0, -1.0);
      CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]);
    
      UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return newImage;
    }
    

    然后这样叫:

    //Rotate it
    UIImage *rotatedImage = [self imageRotatedByDegrees:signatureImage deg:90];
    

    感谢大家.

    2023-02-08 12:12 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有