ios - IOS 中的图像旋转

标签 ios objective-c uiimage uiimageorientation

当我将相机图像转换为 base64 时,我正在使用 Xcode 7.3ojective-c 我的应用程序之一,然后图像将旋转向左 90 度我尝试了很多方法来解决这个问题,但没有一个奏效。

Below are the code:

NSString *data64URLString = [NSString stringWithFormat:@"data:image/png;base64,%@", [UIImageJPEGRepresentation(image, 0) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];

我通过这种方式尝试了所有方向,但它不起作用:

CGImageRef cgRef = image.CGImage;
   image = [[UIImage alloc] initWithCGImage:cgRef scale:1.0 orientation:UIImageOrientationDownMirrored];
    UIImage *originalImage = image;

最佳答案

试试这段代码:

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

关于ios - IOS 中的图像旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37560433/

相关文章:

ios - 为什么 alpha 混合渐变纹理会在 OpenGL ES 2.0 中产生意想不到的结果?

objective-c - NSTokenField 中的自定义标记

iphone - 调用 obtainPermanentIDsForObjects 时获取线程错误

objective-c - 如何修复此错误: Collection <__NSDictionaryM: 0x6000005ab3a0> was mutated while being enumerated?

ios - 使用 EXIF/geotag 信息更新内存中的 iOS UIImage

html - 使用 Safari Mobile 在固定 div 内呈现问题

ios - 如何将一个对象数组的属性分配给另一个字符串数组?

ios - 崩溃 + (UIImage *)imageWithData :(NSData *)data scale:(CGFloat)scale on iOS 8 | EXC_BAD_ACCESS UNKNOWN or KERNEL_INVALID_ADDRESS

ios - 在 tableview 中显示来自 json 文件的图像

ios - 使用 drawrect 自定义 UIView 动画