1

I am using Xcode 7.3 and ojective-c one of my application when I am converting the camera image into base64 then image will be rotation 90 degree left I try so many method to fix this issue but did not working any one.

Below are the code:

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

I try all the orientation by this ways but it's not working:

CGImageRef cgRef = image.CGImage;
   image = [[UIImage alloc] initWithCGImage:cgRef scale:1.0 orientation:UIImageOrientationDownMirrored];
    UIImage *originalImage = image;
Anand Suthar
  • 3,678
  • 2
  • 30
  • 52
Anil Yadav
  • 1,086
  • 7
  • 18

3 Answers3

0

Try this code:

  -  (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;
   }
Rahul
  • 5,594
  • 7
  • 38
  • 92
  • hi #rahul the problem in base64 conversation on ImageView selected image look good but after convert into base64 it will rotate. – Anil Yadav Jun 01 '16 at 06:59
0

As per this thread, PNG format has a known issue with rotation, I would suggest you to try UIImageJPEGRepresentation with data:image/jpg; instead of data:image/png;. This way it will probably set the rotation flag.

Hope it helps!

Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • NSString *data64URLString = [NSString stringWithFormat:@"data:image/jpg;,%@", [UIImageJPEGRepresentation(image, 0) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]]; – Anil Yadav Jun 01 '16 at 07:54
-1

You can save to your app and get image from memory to view with code:

- (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;
}
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53