简体   繁体   English

IOS中的图像旋转

[英]Image rotation In IOS

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. 当我将相机图像转换为base64时,我正在使用Xcode 7.3ojective-c作为我的应用程序之一, 因此图像将向左旋转90度,我尝试了很多方法来解决此问题,但没有任何工作。

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;

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

As per this thread , PNG format has a known issue with rotation, I would suggest you to try UIImageJPEGRepresentation with data:image/jpg; 按照此线程 ,PNG格式在旋转方面存在已知问题,建议您尝试使用data:image/jpg; UIImageJPEGRepresentation data:image/jpg; instead of data:image/png; 而不是data:image/png; . This way it will probably set the rotation flag. 这样,它可能会设置旋转标志。

Hope it helps! 希望能帮助到你!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM