Iphone 在服务器上上载时出现UIImageOrientation问题

Iphone 在服务器上上载时出现UIImageOrientation问题,iphone,objective-c,ipad,uiimage,orientation,Iphone,Objective C,Ipad,Uiimage,Orientation,我不知道这是怎么发生的,但当我在纵向模式下从相机拍摄图像并上传到服务器时,它显示良好,但当我从照片库拍摄相同的图像并上传到服务器时,它显示为横向模式 不知道这是怎么发生的?在过去的5个小时里,我一直被困在那里 我经历过,但还没有成功 有人帮我解决这个问题吗 提前谢谢 更新 - (UIImage *)imageToFitSize:(CGSize)fitSize method:(MGImageResizingMethod)resizeMethod { float imageScaleFact

我不知道这是怎么发生的,但当我在纵向模式下从相机拍摄图像并上传到服务器时,它显示良好,但当我从照片库拍摄相同的图像并上传到服务器时,它显示为横向模式

不知道这是怎么发生的?在过去的5个小时里,我一直被困在那里

我经历过,但还没有成功

有人帮我解决这个问题吗

提前谢谢

更新

- (UIImage *)imageToFitSize:(CGSize)fitSize method:(MGImageResizingMethod)resizeMethod
{
    float imageScaleFactor = 1.0;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
    if ([self respondsToSelector:@selector(scale)]) {
        imageScaleFactor = [self scale];
    }
#endif

    float sourceWidth = [self size].width * imageScaleFactor;
    float sourceHeight = [self size].height * imageScaleFactor;
    float targetWidth = fitSize.width;
    float targetHeight = fitSize.height;
    BOOL cropping = !(resizeMethod == MGImageResizeScale);

    // Calculate aspect ratios
    float sourceRatio = sourceWidth / sourceHeight;
    float targetRatio = targetWidth / targetHeight;

    // Determine what side of the source image to use for proportional scaling
    BOOL scaleWidth = (sourceRatio <= targetRatio);
    // Deal with the case of just scaling proportionally to fit, without cropping
    scaleWidth = (cropping) ? scaleWidth : !scaleWidth;

    // Proportionally scale source image
    float scalingFactor, scaledWidth, scaledHeight;
    if (scaleWidth) {
        scalingFactor = 1.0 / sourceRatio;
        scaledWidth = targetWidth;
        scaledHeight = round(targetWidth * scalingFactor);
    } else {
        scalingFactor = sourceRatio;
        scaledWidth = round(targetHeight * scalingFactor);
        scaledHeight = targetHeight;
    }
    float scaleFactor = scaledHeight / sourceHeight;

    // Calculate compositing rectangles
    CGRect sourceRect, destRect;
    if (cropping) {
        destRect = CGRectMake(0, 0, targetWidth, targetHeight);
        float destX, destY;
        if (resizeMethod == MGImageResizeCrop) {
            // Crop center
            destX = round((scaledWidth - targetWidth) / 2.0);
            destY = round((scaledHeight - targetHeight) / 2.0);
        } else if (resizeMethod == MGImageResizeCropStart) {
            // Crop top or left (prefer top)
            if (scaleWidth) {
                // Crop top
                destX = 0.0;
                destY = 0.0;
            } else {
                // Crop left
                destX = 0.0;
                destY = round((scaledHeight - targetHeight) / 2.0);
            }
        } else if (resizeMethod == MGImageResizeCropEnd) {
            // Crop bottom or right
            if (scaleWidth) {
                // Crop bottom
                destX = round((scaledWidth - targetWidth) / 2.0);
                destY = round(scaledHeight - targetHeight);
            } else {
                // Crop right
                destX = round(scaledWidth - targetWidth);
                destY = round((scaledHeight - targetHeight) / 2.0);
            }
        }
        sourceRect = CGRectMake(destX / scaleFactor, destY / scaleFactor,
                                targetWidth / scaleFactor, targetHeight / scaleFactor);
    } else {
        sourceRect = CGRectMake(0, 0, sourceWidth, sourceHeight);
        destRect = CGRectMake(0, 0, scaledWidth, scaledHeight);
    }

    // Create appropriately modified image.
    UIImage *image = nil;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) {
        UIGraphicsBeginImageContextWithOptions(destRect.size, NO, 0.0); // 0.0 for scale means "correct scale for device's main screen".

        CGImageRef sourceImg;

        if(resizeMethod == MGImageResizeCrop)
            sourceImg = CGImageCreateWithImageInRect([self CGImage], sourceRect); // cropping happens here.
        else 
            sourceImg = CGImageRetain([self CGImage]);       // scaling happens here.

        image = [UIImage imageWithCGImage:sourceImg scale:0.0 orientation:self.imageOrientation]; // create cropped UIImage.
        [image drawInRect:destRect]; // the actual scaling happens here, and orientation is taken care of automatically.
        CGImageRelease(sourceImg);
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
#endif
    if (!image) {
        // Try older method.
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = CGBitmapContextCreate(NULL, fitSize.width, fitSize.height, 8, (fitSize.width * 4),
                                                     colorSpace, kCGImageAlphaPremultipliedLast);

        CGImageRef sourceImg;
        if(resizeMethod == MGImageResizeCrop)
            sourceImg = CGImageCreateWithImageInRect([self CGImage], sourceRect); // cropping happens here.
        else 
            sourceImg = CGImageRetain([self CGImage]);       // scaling happens here.

        //CGImageRef sourceImg = CGImageCreateWithImageInRect([self CGImage], sourceRect);
        CGContextDrawImage(context, destRect, sourceImg);
        CGImageRelease(sourceImg);
        CGImageRef finalImage = CGBitmapContextCreateImage(context);
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        image = [UIImage imageWithCGImage:finalImage];
        CGImageRelease(finalImage);
    }

    return image;
}
-(UIImage*)图像大小:(CGSize)fitSize方法:(MGImageResizingMethod)resizeMethod
{
浮点imageScaleFactor=1.0;
#如果允许的IPHONE操作系统版本最大值>=40000
if([自响应选择器:@选择器(比例)]){
imageScaleFactor=[自缩放];
}
#恩迪夫
float sourceWidth=[self size].width*imageScaleFactor;
float sourceHeight=[self size].height*imageScaleFactor;
浮动目标宽度=fitSize.width;
浮动目标灯=fitSize.height;
BOOL裁剪=!(resizeMethod==MGImageResizeScale);
//计算纵横比
浮动源比率=源宽度/源高度;
浮动目标比率=目标宽度/目标高度;
//确定源图像的哪一侧用于比例缩放
布尔标度宽度=(源比率=40000)
如果([[[UIDevice currentDevice]systemVersion]floatValue]>=4.0){
UIGraphicsBeginImageContextWithOptions(desect.size,NO,0.0);//0.0表示“设备主屏幕的正确比例”。
CGImageRef-sourceImg;
如果(resizeMethod==MGImageResizeCrop)
sourceImg=CGImageCreateWithImageInRect([self-CGImage],sourceRect);//此处进行裁剪。
其他的
sourceImg=cgimagerain([self-CGImage]);//此处发生缩放。
image=[UIImage imageWithCGImage:sourceImg比例:0.0方向:self.imageOrientation];//创建裁剪过的UIImage。
[image drawInRect:destRect];//实际的缩放发生在这里,方向会自动处理。
CGImageRelease(sourceImg);
image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
}
#恩迪夫
如果(!图像){
//试试旧的方法。
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
CGContextRef context=CGBitmapContextCreate(NULL,fitSize.width,fitSize.height,8,(fitSize.width*4),
色彩空间,KCGIMAGEAlphaPremultipledLast);
CGImageRef-sourceImg;
如果(resizeMethod==MGImageResizeCrop)
sourceImg=CGImageCreateWithImageInRect([self-CGImage],sourceRect);//此处进行裁剪。
其他的
sourceImg=cgimagerain([self-CGImage]);//此处发生缩放。
//CGImageRef sourceImg=CGImageCreateWithImageInRect([self CGImage],sourceRect);
CGContextDrawImage(上下文、析取、源img);
CGImageRelease(sourceImg);
CGImageRef finalImage=CGBitmapContextCreateImage(上下文);
CGContextRelease(上下文);
CGCOLORSPACTERELEASE(色彩空间);
图像=[UIImage imageWithCGImage:finalImage];
CGImageRelease(finalImage);
}
返回图像;
}

其中,
MGImageResizingMethod
是我定义的枚举,并在函数中将
MGImageResizeScale
作为参数传递。

试试看,它可能有效。您必须根据图像设置条件

if(image == fromCamera){
   [image fixOrientation];
}
else{
//please do not convert it to orientation 
}

试试看,它可能会起作用。你必须根据你的图像设置条件

if(image == fromCamera){
   [image fixOrientation];
}
else{
//please do not convert it to orientation 
}

我建议你访问这两个参考,你会得到解决方案


希望,这将对您有所帮助。

我建议您访问这两个参考资料,您将得到解决方案


希望,这将帮助您..

只做缩放而不旋转..@CocoaMatters查看更新的答案只做缩放而不旋转..@CocoaMatters查看更新的答案