iOS版:图像获取旋转保存为PNG表示数据后90度iOS版:图像获取旋转保存为PNG表示数据后90度

2019-05-16 21:25发布

我研究够得到这个工作,但没能解决它。 拍照从相机,只要我有图像存储的UIImage后,它的罚款,但只要我这个存储的图像作为PNG表示,其get旋转90度。

以下是我的代码,所有的事情我想:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];

    if([mediaType isEqualToString:(NSString*)kUTTypeImage]) 
    {
        AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        delegate.originalPhoto  = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSLog(@"Saving photo");
        [self saveImage];
        NSLog(@"Fixing orientation");
        delegate.fixOrientationPhoto  = [self fixOrientation:[UIImage imageWithContentsOfFile:[delegate filePath:imageName]]];      
        NSLog(@"Scaling photo");
        delegate.scaledAndRotatedPhoto  =  [self scaleAndRotateImage:[UIImage imageWithContentsOfFile:[delegate filePath:imageName]]];
    }
    [picker dismissModalViewControllerAnimated:YES];
    [picker release];
}


- (void)saveImage
{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSData *imageData = UIImagePNGRepresentation(delegate.originalPhoto);
    [imageData writeToFile:[delegate filePath:imageName] atomically:YES];
}

这里取自fixOrientation和scaleAndRotateImage功能在这里和这里分别。 他们工作得很好,当我申请他们的UIImage但如果我保存图像为PNG表示并应用它们不起作用旋转图像。

请参考,下面的图片上述功能执行后:

Answer 1:

与iOS 4.0开始,当相机拍摄照片不会保存前将其旋转,它

简单地设置在JPEG.If的EXIF数据,你一个UIImage保存为JPEG旋转标志,它

将设置旋转flag.PNGs不支持旋转标志,所以如果你一个UIImage保存为

PNG,它会被错误地和不旋转设置一个标志来修复它。 所以,如果你想PNG

图片你必须自己旋转它们,对于检查这个链接 。



Answer 2:

雨燕3.1版本的UIImage扩展张贴饶:

extension UIImage {
    func fixOrientation() -> UIImage {
        if self.imageOrientation == UIImageOrientation.up {
            return self
        }
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
        if let normalizedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() {
            UIGraphicsEndImageContext()
            return normalizedImage
        } else {
            return self
        }
    }
}

用法:

let cameraImage = //image captured from camera
let orientationFixedImage = cameraImage.fixOrientation()


Answer 3:

对于雨燕2.1

添加如下的UIImage扩展,

extension UIImage {
   func fixOrientation() -> UIImage {
        if self.imageOrientation == UIImageOrientation.Up {
            return self
        }

        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        self.drawInRect(CGRectMake(0, 0, self.size.width, self.size.height))
        let normalizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return normalizedImage;
    }
}

实例:

let cameraImage = //image captured from camera
let orientationFixedImage = cameraImage.fixOrientation()


Answer 4:

我发现下面的提示是巨大有用:

1.自然输出是风景

2. .WIDTH / .height ARE受.imageOrientation

3使用短尺寸而非.WIDTH


(1)相机的剧照“自然”输出为横向。

这是违反直觉的。 画像是的UIImagePickerController等提供的唯一方法,但是使用“正常”的肖像相机时,你会得到UIImageOrientationRight为“正常”的方向

(就我记得这个 - 视频自然输出(景观课程);所以剧照都是一样的 - 尽管iPhone是所有关于画像。)


(2).WIDTH和.height 由.imageOrientation !!!!!!!!!确实影响

一定要做到这一点,并尝试在iPhone上左右逢源,

NSLog(@"fromImage.imageOrientation is %d", fromImage.imageOrientation);
NSLog(@"fromImage.size.width  %f   fromImage.size.height  %f",
            fromImage.size.width, fromImage.size.height);

你会看到,.height和.WIDTH交换“即使”真正的像素景观。


(3)简单地使用“短尺寸”,而不是.WIDTH, 往往能解决许多问题

我发现这是难以置信的帮助。 假设你想也许是图像的顶部方:

CGRect topRectOfOriginal = CGRectMake(0,0, im.size.width,im.size.width);

实际上是行不通的,你会得到一个压扁的图像,当相机(“真”)正在举行的风景线。

但是如果你很简单地做到这一点

float shortDimension = fminf(im.size.width, im.size.height);
CGRect topRectOfOriginal = CGRectMake(0,0, shortDimension,shortDimension);

那么“一切都是固定的”,你实际上是“不必担心”的方向标志。 再次指向(3)不是万灵药,但它往往不会解决所有的问题。

希望它可以帮助别人节省一些时间。



Answer 5:

我发现这个代码在这里 ,这实际上固定对我来说。 对于我的应用程序,我拍了一张照片,并保存它,每次我装好了,它会附着恼人的旋转(我抬头一看,这是很明显的东西做的EXIF和iPhone采用的方式,将影像存储)。 此代码固定对我来说。 我不得不说,它最初是作为除了类/扩展/类别(你可以找到链接原件。我用它象下面这样一个简单的方法,因为我真的不希望做一个整体类或类别的只是这个。我只用了肖像,但我认为代码适用于任何方向。我不知道,虽然

咆哮过,下面的代码:

- (UIImage *)fixOrientationForImage:(UIImage*)neededImage {

    // No-op if the orientation is already correct
    if (neededImage.imageOrientation == UIImageOrientationUp) return neededImage;

    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    CGAffineTransform transform = CGAffineTransformIdentity;

    switch (neededImage.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, neededImage.size.width, neededImage.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, neededImage.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;

        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, neededImage.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationUpMirrored:
            break;
    }

    switch (neededImage.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, neededImage.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;

        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, neededImage.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationLeft:
        case UIImageOrientationRight:
            break;
    }

    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, neededImage.size.width, neededImage.size.height,
                                             CGImageGetBitsPerComponent(neededImage.CGImage), 0,
                                             CGImageGetColorSpace(neededImage.CGImage),
                                             CGImageGetBitmapInfo(neededImage.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (neededImage.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,neededImage.size.height,neededImage.size.width), neededImage.CGImage);
            break;

        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,neededImage.size.width,neededImage.size.height), neededImage.CGImage);
            break;
    }

    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}

我不知道这是多么有用将是你的,但我希望它帮助:)



Answer 6:

试试这个,你可以使用

NSData *somenewImageData = UIImageJPEGRepresentation(newimg,1.0);

代替

NSData *somenewImageData = UIImagePNGRepresentation(newimg);


Answer 7:

请尝试以下代码

UIImage *sourceImage = ... // Our image
CGRect selectionRect = CGRectMake(100.0, 100.0, 300.0, 400.0);
CGImageRef resultImageRef = CGImageCreateWithImageInRect(sourceImage.CGImage,
selectionRect);
UIImage *resultImage = [[UIImage alloc] initWithCGImage:resultImageRef];

CGRect TransformCGRectForUIImageOrientation(CGRect source, UIImageOrientation orientation, CGSize imageSize) {
switch (orientation) {
case UIImageOrientationLeft: { // EXIF #8
  CGAffineTransform txTranslate = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
  CGAffineTransform txCompound = CGAffineTransformRotate(txTranslate,M_PI_2);
  return CGRectApplyAffineTransform(source, txCompound);
}
case UIImageOrientationDown: { // EXIF #3
  CGAffineTransform txTranslate = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
  CGAffineTransform txCompound = CGAffineTransformRotate(txTranslate,M_PI);
  return CGRectApplyAffineTransform(source, txCompound);
}
case UIImageOrientationRight: { // EXIF #6
  CGAffineTransform txTranslate = CGAffineTransformMakeTranslation(0.0, imageSize.width);
  CGAffineTransform txCompound = CGAffineTransformRotate(txTranslate,M_PI + M_PI_2);
  return CGRectApplyAffineTransform(source, txCompound);
}
case UIImageOrientationUp: // EXIF #1 - do nothing
  default: // EXIF 2,4,5,7 - ignore
  return source;
}
}
  ...
UIImage *sourceImage = ... // Our image
 CGRect selectionRect = CGRectMake(100.0, 100.0, 300.0, 400.0);
CGRect transformedRect = TransformCGRectForUIImageOrientation(selectionRect,  sourceImage.imageOrientation, sourceImage.size);
CGImageRef resultImageRef = CGImageCreateWithImageInRect(sourceImage.CGImage, transformedRect);
UIImage *resultImage = [[UIImage alloc] initWithCGImage:resultImageRef];

我从以下链接referanced已经寻找更多的细节

最好的祝福 :-)



Answer 8:

试试这个代码:

NSData *imageData = UIImageJPEGRepresentation(delegate.originalPhoto,100);


文章来源: iOS: Image get rotated 90 degree after saved as PNG representation data