为了账号安全,请及时绑定邮箱和手机立即绑定

上传后IOS UIImagePickerController结果图像定向

上传后IOS UIImagePickerController结果图像定向

iOS
Helenr 2019-06-14 17:13:44
上传后IOS UIImagePickerController结果图像定向我正在iOS3.1.3 iPhone上测试我的iPhone应用程序。我使用UIImagePickerController:UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];[imagePicker setSourceType: UIImagePickerControllerSourceTypeCamera];[imagePicker setDelegate:self];[self.navigationController presentModalViewController: imagePicker animated:YES];[imagePicker release];- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info {     self.image = [info objectForKey:UIImagePickerControllerOriginalImage];     imageView.image = self.image;     [self.navigationController dismissModalViewControllerAnimated:YES];     submitButton.enabled = YES;}然后在某个时候使用ASI类将其发送到我的Web服务器:ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"  [request setDelegate:self];[request setStringEncoding:NSUTF8StringEncoding];[request setShouldContinueWhenAppEntersBackground:YES]; //other post keys/values[request setFile:UIImageJPEGRepresentation(self.image, 100.0f) withFileName:[NSString stringWithFormat:@"%d.jpg",  [[NSDate date] timeIntervalSinceNow]] andContentType:@"image/jpg" forKey:@"imageFile"];[request startAsynchronous];问题是:当我和iPhone合影时,图像会上传到服务器上,并按您的预期进行查看。当拍照时,拿着手机的肖像,图像上传和查看,因为它已经旋转了90度。我的应用程序被设置为只工作在纵向模式(倒置和常规)。如何使图片上传后始终显示正确的方向?在UIImageView中显示的图像似乎是正确的(在拍照后直接显示),但服务器上的查看则不然。
查看完整描述

3 回答

?
慕姐4208626

TA贡献1852条经验 获得超7个赞

我想出了一个简单得多的方法:

- (UIImage *)normalizedImage {
    if (self.imageOrientation == UIImageOrientationUp) return self; 

    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
    [self drawInRect:(CGRect){0, 0, self.size}];
    UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return normalizedImage;}

顺便说一句:@失范的代码不需要scale考虑到,所以不会工作2倍的图像。


查看完整回答
反对 回复 2019-06-14
?
料青山看我应如是

TA贡献1772条经验 获得超8个赞

以下是@an 0的答案的一个SWIFT版本:

func normalizedImage() -> UIImage {

  if (self.imageOrientation == UIImageOrientation.Up) { 
      return self;
  }

  UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale);
  let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
  self.drawInRect(rect)

  let normalizedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext();
  return normalizedImage;}

还有一个更一般的职能:

func fixOrientation(img:UIImage) -> UIImage {

  if (img.imageOrientation == UIImageOrientation.Up) { 
      return img;
  }

  UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale);
  let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)
  img.drawInRect(rect)

  let normalizedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext();
  return normalizedImage;}

SWIFT 3版本:

func fixOrientation(img: UIImage) -> UIImage {
    if (img.imageOrientation == .up) {
        return img    }

    UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale)
    let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)
    img.draw(in: rect)

    let normalizedImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    return normalizedImage}


查看完整回答
反对 回复 2019-06-14
?
慕丝7291255

TA贡献1859条经验 获得超6个赞

UIImage有财产imageOrientation,它指示UIImageView和其他UIImage使用者旋转原始图像数据。这个标志很有可能被保存到上传的jpeg图像中的EXIF数据,但是您用来查看它的程序没有遵守该标志。

旋转UIImage若要在上载时正确显示,可以使用以下类别:

UIImage+FixOrientOriented.h

@interface UIImage (fixOrientation)- (UIImage *)fixOrientation;@end

UIImage+FixOrientOriented.m

@implementation UIImage (fixOrientation)- (UIImage *)fixOrientation {

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

    // 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 (self.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

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

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

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

        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, self.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, self.size.width, self.size.height,
                                             CGImageGetBitsPerComponent(self.CGImage), 0,
                                             CGImageGetColorSpace(self.CGImage),
                                             CGImageGetBitmapInfo(self.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (self.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
            break;

        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.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;}@end


查看完整回答
反对 回复 2019-06-14
  • 3 回答
  • 0 关注
  • 581 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信