1

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

下面是代码:

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;
4

3 回答 3

0

试试这个代码:

  -  (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;
   }
于 2016-06-01T06:42:19.063 回答
0

根据这个线程,PNG 格式有一个已知的旋转问题,我建议你尝试UIImageJPEGRepresentation使用data:image/jpg;而不是data:image/png;. 这样它可能会设置旋转标志。

希望能帮助到你!

于 2016-06-01T07:18:07.503 回答
-1

您可以保存到您的应用程序并从内存中获取图像以使用代码查看:

- (UIImage *)finishSavePhotoWithImagePickerController:(NSDictionary *()info {

    UIImage *editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
    UIImage *originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];

    if (editedImage) {
        imageToSave = editedImage;
    } else {
        imageToSave = originalImage;
    }

    NSData *imageData = UIImageJPEGRepresentation(imageToSave, 1.0);
    [imageData writeToFile:@"yourFilePath" options:NSDataWritingFileProtectionNone error:nil];
    UIImage *showImage = [[UIImage alloc] initWithContentsOfFile:@"yourFilePath"];
    return showImage;
}
于 2016-06-01T07:55:04.270 回答