在服务器上传图像,接收旋转后的图像
在我的应用程序中,我想将图像上传到服务器,并检索上传图像的URL以将其显示在应用程序中。我发送图像的NSData。这是我的代码:在服务器上传图像,接收旋转后的图像
AsynchronousImageView *imgVw_User = (AsynchronousImageView*)[self.view viewWithTag:K_TagImageViewUser];
UIImage *UserImgForBioData = imgVw_User.image;
UserImgForBioData = [UIImage imageWithCGImage:UserImgForBioData.CGImage scale:1.0f orientation:UserImgForBioData.imageOrientation];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(UserImgForBioData,0.5)];
Base64Transcoder *temp_transcoder=[[Base64Transcoder alloc]init];
NSString * temp_base64EncodedImage= @"";
if (imageData!=nil)
{
temp_base64EncodedImage = [NSString stringWithFormat:@"%@",[temp_transcoder base64EncodedStringfromData:imageData]];
temp_base64EncodedImage = [[temp_base64EncodedImage stringByReplacingOccurrencesOfString:@"\n"
withString:@""]
mutableCopy];
}
有时当我显示图像时,通过获取图像的url,图像被旋转。难道我做错了什么?
如果[R从摄像机捕捉图像,然后上传图像通过该生成图像缩略图在原来的方向前:在此之后
- (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;
}
u得到正确的图像,然后上传图片。我希望这会解决你的问题。
第1步:从相机捕捉图像。
第二步:然后上传所得到的图像:通过以下方法
第三步生成photoThumbnail。
使用从照片库中的图像时,你会得到正确的图像
谢谢你。代码的工作就像一个魅力。为了参考其他人,我会再次在这里写上述评论::从相机捕获图像后,通过该图像生成photumbumbnail,然后你会得到一个实际的图像。对于照片缩略图,我已经在下面给你提供了一种方法。实际上原始图像的方向不正确,下面的方法会调整图像的实际方向。第1步:从相机捕捉图像。第2步:通过以下方法生成photoThumbnail第3步:然后上传合成图像。你会得到正确的图像 – NiKKi
欢迎.... :) –
谢谢!完美的作品!我更新了Swift的代码https://gist.github.com/dc7d8c90195bbeab3e32.git – Madman
这为我工作。
ALAssetRepresentation *rep = [_asset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
UIImage* img = [UIImage imageWithCGImage:iref scale:1.f orientation:UIImageOrientationUp]
注意:_asset是ALAsset的一个实例。
你从相机捕获图像? –
问题出现在服务器上的可能性很大,可能不考虑图像方向标志,并将它们存储在旋转的服务器上。 –
@SatishKAzad - 是的。当我使用相机中的图像,甚至是从相机点击的图像库时,问题就出现了。 – NiKKi