gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 04:08:51 24 4
gpt4 key购买 nike

我已经阅读了很多与此相关的答案,但我仍然无法让它工作。

我有一个 View ,用户可以在其中签名。它是这样的: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];

如何在保存前旋转图像?

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

最佳答案

我终于使用这篇文章中的一个答案来解决这个问题:How to Rotate a UIImage 90 degrees?

我用的是这个方法:

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

谢谢大家。

关于ios - 在另存为 PNG 之前将新创建的 iOS 图像旋转 90 度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20764623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com