【问题标题】:Image rotation In IOSIOS中的图像旋转
【发布时间】:2016-09-30 08:03:36
【问题描述】:

当我将 相机图像转换为 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;

【问题讨论】:

  • 您是如何发现必须将UIImageJPEGRepresentationdata:image/png 一起使用的??
  • 嗨 #NeverHopeless 我在 stackoverflow 数据上发现了这个:image/png 和 jpeg 也是。
  • base64 转换中的问题,因为在将捕获图像转换为 base64 后,它会旋转图像位置,但如果我从图库中选择,它会正常工作。
  • 你试过UIImageJPEGRepresentation 和data:image/**jpg**
  • 是的,我试过了,不行

标签: ios objective-c uiimage uiimageorientation


【解决方案1】:

试试这个代码:

  -  (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 ImageView 所选图像上 base64 对话中的问题看起来不错,但转换为 base64 后它会旋转。
【解决方案2】:

根据this thread,PNG 格式存在一个已知的旋转问题,我建议您尝试使用UIImageJPEGRepresentationdata:image/jpg; 而不是data:image/png;。这样它可能会设置旋转标志。

希望对你有帮助!

【讨论】:

  • NSString *data64URLString = [NSString stringWithFormat:@"data:image/jpg;,%@", [UIImageJPEGRepresentation(image, 0) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 2012-03-17
    • 2018-03-27
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多