Uploading image on server, receiving rotated image

前端 未结 2 655
不思量自难忘°
不思量自难忘° 2020-12-11 11:19

In my application I want to upload the image to the server and retrieve the url of uploaded image to display it in the application. I send the NSData of image. Here is my co

相关标签:
2条回答
  • 2020-12-11 11:38

    This worked for me when using images from the the Photo library.

    ALAssetRepresentation *rep = [_asset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    UIImage* img = [UIImage imageWithCGImage:iref scale:1.f orientation:UIImageOrientationUp]
    

    Note: _asset is an instance of ALAsset.

    0 讨论(0)
  • 2020-12-11 11:58

    If you r capturing Image from Camera then before uploading image generate Image Thumbnail in original orientation by this:

    - (UIImage *)generatePhotoThumbnail:(UIImage *)image 
    {
        //int kMaxResolution = 320; 
    
        CGImageRef imgRef = image.CGImage; 
    
        CGFloat width = CGImageGetWidth(imgRef); 
        CGFloat height = CGImageGetHeight(imgRef); 
    
        CGAffineTransform transform = CGAffineTransformIdentity; 
        CGRect bounds = CGRectMake(0, 0, width, height); 
        /*if (width > kMaxResolution || height > kMaxResolution) 
         { 
         CGFloat ratio = width/height; 
         if (ratio > 1)
         { 
         bounds.size.width = kMaxResolution; 
         bounds.size.height = bounds.size.width / ratio; 
         } 
         else
         { 
         bounds.size.height = kMaxResolution; 
         bounds.size.width = bounds.size.height * ratio; 
         } 
         } */
    
        CGFloat scaleRatio = bounds.size.width / width; 
        CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));  
        CGFloat boundHeight;                       
        UIImageOrientation orient = image.imageOrientation;                         
        switch(orient)
        { 
            case UIImageOrientationUp: //EXIF = 1 
                transform = CGAffineTransformIdentity; 
                break;
    
            case UIImageOrientationUpMirrored: //EXIF = 2  
                transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0); 
                transform = CGAffineTransformScale(transform, -1.0, 1.0); 
                break; 
    
            case UIImageOrientationDown: //EXIF = 3 
                transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height); 
                transform = CGAffineTransformRotate(transform, M_PI); 
                break; 
    
            case UIImageOrientationDownMirrored: //EXIF = 4 
                transform = CGAffineTransformMakeTranslation(0.0, imageSize.height); 
                transform = CGAffineTransformScale(transform, 1.0, -1.0); 
                break; 
    
            case UIImageOrientationLeftMirrored: //EXIF = 5 
                boundHeight = bounds.size.height; 
                bounds.size.height = bounds.size.width; 
                bounds.size.width = boundHeight; 
                transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width); 
                transform = CGAffineTransformScale(transform, -1.0, 1.0); 
                transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0); 
                break; 
    
            case UIImageOrientationLeft: //EXIF = 6 
                boundHeight = bounds.size.height; 
                bounds.size.height = bounds.size.width; 
                bounds.size.width = boundHeight; 
                transform = CGAffineTransformMakeTranslation(0.0, imageSize.width); 
                transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0); 
                break; 
    
            case UIImageOrientationRightMirrored: //EXIF = 7 
                boundHeight = bounds.size.height; 
                bounds.size.height = bounds.size.width; 
                bounds.size.width = boundHeight; 
                transform = CGAffineTransformMakeScale(-1.0, 1.0); 
                transform = CGAffineTransformRotate(transform, M_PI / 2.0); 
                break; 
    
            case UIImageOrientationRight: //EXIF = 8 
                boundHeight = bounds.size.height; 
                bounds.size.height = bounds.size.width; 
                bounds.size.width = boundHeight; 
                transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0); 
                transform = CGAffineTransformRotate(transform, M_PI / 2.0); 
                break; 
            default: 
                [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"]; 
                break;
        } 
    
        UIGraphicsBeginImageContext(bounds.size); 
    
        CGContextRef context = UIGraphicsGetCurrentContext(); 
    
        if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft)
        { 
            CGContextScaleCTM(context, -scaleRatio, scaleRatio); 
            CGContextTranslateCTM(context, -height, 0); 
        } 
        else
        { 
            CGContextScaleCTM(context, scaleRatio, -scaleRatio); 
            CGContextTranslateCTM(context, 0, -height); 
        } 
    
        CGContextConcatCTM(context, transform); 
    
        CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); 
        UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 
        UIGraphicsEndImageContext(); 
    
        return imageCopy;
    
    }
    

    after this u get a correct image and then upload that image. I hope this will solve your issue.

    Step1: capture image from camera.

    Step2: Generate photoThumbnail by following method

    Step3: Then upload the resultant image.

    you will get correct image

    0 讨论(0)
提交回复
热议问题