亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

上傳后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 關注
  • 607 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號