我正在尝试将相机图像从iPhone上传到我的网络服务器,但是当我从服务器下载时,图像会如何改变它的方向。这只发生在相机图像而不是从我的机器上传到设备的简单图像.Plz帮助我
我已使用此条件在服务器端旋转图像:
// obj将在服务器上传图像时传递Orientation,其值将为1或3或6或8
switch ((ushort)obj)
{
case 1: //up
return 0;
case 3: //down
return 180;
case 6: //left
return 90;
case 8: //right
return 270;
default:
return 0;
}
但我仍然遇到同样的问题
以下是我上传图片的代码:
-(void)uploadSingleFile
{
if(!self.objWebServices.bSyncWasOn)
{
NSLog(@"STOP upload Single File RETURN");
[self Call_SaveSyncedImageID];
return;
}
SyncItem* item = [arrSelSyncItemIDS objectAtIndex:nSyncItemIndex];
if(item.nType == SYNC_STATUS_NOT_SELECTED || item.nType == SYNC_STATUS_SYNC_DONE)
{
nSyncItemIndex = nSyncItemIndex + 1;
if([arrSelSyncItemIDS count] > nSyncItemIndex)
{
NSLog(@"Called uploadSingleFile");
[self uploadSingleFile];
return;
}
else
{
//[self LoadImages:!bShowHideSyncImages];
[SVProgressHUD dismiss];
[BtnStopSync setTitle:@"Start sync" forState:UIControlStateNormal];
self.objWebServices.bSyncWasOn = NO;
self.objWebServices.isFileUpload = NO;
if( [[NSUserDefaults standardUserDefaults] boolForKey:@"Notification_State"] ) // Setting Switch Bool
{
UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"winkflash" message:@"*** Sync completed successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[Alert show];
}
}
}
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *srtprefSessionId = [NSString stringWithFormat:@"%@",[prefs objectForKey:@"prefSessionId"]];
BOOL isTrash = FALSE;
NSString *StrPath = @"/Pictures/"; //Change
ALAsset* temp = item.itemAsset;
NSDate* assetDate = (NSDate*)[temp valueForProperty:@"ALAssetPropertyDate"];
NSString* strID = @"";
strID = [[temp valueForProperty:@"ALAssetPropertyURLs"] description];
strID = [strID stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString* strImageType = @"";
if ([strID rangeOfString:@"jpg"].location != NSNotFound || [strID rangeOfString:@"JPG"].location != NSNotFound ){
strImageType = @"jpg";
}
else if ([strID rangeOfString:@"png"].location != NSNotFound || [strID rangeOfString:@"PNG"].location != NSNotFound )
strImageType = @"png";
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"MM/dd/yyyy"];
NSString *result = [df stringFromDate:assetDate];
NSTimeInterval timeInterval = [assetDate timeIntervalSince1970];
ALAssetRepresentation *rep = [temp defaultRepresentation];
//------------------ metadata -------------------------------------------------------
NSDictionary *imageMetadata = [rep metadata];
NSString *strOrt=[NSString stringWithFormat:@"%@",[imageMetadata valueForKey:@"Orientation"]];
NSString *strOrientation;
if([strOrt isEqualToString:@"6"])
{
strOrientation=@"1";
}
if([strOrt isEqualToString:@"8"])
{
strOrientation=@"3";
}
if([strOrt isEqualToString:@"3"])
{
strOrientation=@"6";
}
if([strOrt isEqualToString:@"1"])
{
strOrientation=@"8";
}
NSLog(@"print strOrientation:%@",strOrientation);
//-----------------------------------------------------------------------------------
StrPath = [StrPath stringByAppendingFormat:@"%d.%@",(int)timeInterval,strImageType];
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
NSData *dataObj = nil;
dataObj = UIImagePNGRepresentation(image);
NSString* StrFileData = [Base64 encode:dataObj];
NSString* strFileHash = [dataObj md5Test];
//----------------------------------------------------
NSMutableDictionary *DictRequest = [[NSMutableDictionary alloc]init];
[DictRequest setObject:srtprefSessionId forKey:@"SessionId"];
[DictRequest setObject:StrPath forKey:@"Path"];
[DictRequest setValue:[NSNumber numberWithBool:isTrash] forKey:@"UploadDirectlyToTrashbin"];
[DictRequest setObject:StrFileData forKey:@"FileData"];
[DictRequest setObject:strFileHash forKey:@"FileHash"];
[DictRequest setObject:result forKey:@"DateCreated"];
BOOL isNULL = [self stringIsEmpty:strOrientation];
if(!isNULL)
{
[DictRequest setObject:strOrientation forKey:@"Orientation"];
}
NSString *jsonString = [DictRequest JSONRepresentation];
NSString *strUrl=[NSString stringWithFormat:@"%@",FileUpload_URL];
NSURL *url1=[NSURL URLWithString:strUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url1];
[request setTimeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSData *postData = [jsonString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
[request setHTTPBody:postData];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
if(theConnection)
[self Set2Defaults];
theConnection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[SVProgressHUD dismiss];
if(theConnection)
webData = [NSMutableData data];
else
NSLog(@"Connection Failed !!!");
}
答案 0 :(得分:8)
这是由于方向存储在UIImage中的方式。
我在UIImage的类别中使用此功能。
- (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;
}
从imagePicker获取图像时运行此命令。
然后,当您上传和下载时,它将保持方向。
答案 1 :(得分:2)
试试这个:
UIImage *sourceImage = ... // Our image
CGRect selectionRect = CGRectMake(100.0, 100.0, 300.0, 400.0);
CGImageRef resultImageRef = CGImageCreateWithImageInRect(sourceImage.CGImage,
selectionRect);
UIImage *resultImage = [[UIImage alloc] initWithCGImage:resultImageRef];
和
CGRect TransformCGRectForUIImageOrientation(CGRect source, UIImageOrientation orientation, CGSize imageSize) {
switch (orientation) {
case UIImageOrientationLeft: { // EXIF #8
CGAffineTransform txTranslate = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
CGAffineTransform txCompound = CGAffineTransformRotate(txTranslate,M_PI_2);
return CGRectApplyAffineTransform(source, txCompound);
}
case UIImageOrientationDown: { // EXIF #3
CGAffineTransform txTranslate = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
CGAffineTransform txCompound = CGAffineTransformRotate(txTranslate,M_PI);
return CGRectApplyAffineTransform(source, txCompound);
}
case UIImageOrientationRight: { // EXIF #6
CGAffineTransform txTranslate = CGAffineTransformMakeTranslation(0.0, imageSize.width);
CGAffineTransform txCompound = CGAffineTransformRotate(txTranslate,M_PI + M_PI_2);
return CGRectApplyAffineTransform(source, txCompound);
}
case UIImageOrientationUp: // EXIF #1 - do nothing
default: // EXIF 2,4,5,7 - ignore
return source;
}
}
...
UIImage *sourceImage = ... // Our image
CGRect selectionRect = CGRectMake(100.0, 100.0, 300.0, 400.0);
CGRect transformedRect = TransformCGRectForUIImageOrientation(selectionRect, sourceImage.imageOrientation, sourceImage.size);
CGImageRef resultImageRef = CGImageCreateWithImageInRect(sourceImage.CGImage, transformedRect);
UIImage *resultImage = [[UIImage alloc] initWithCGImage:resultImageRef];