I am trying to upload camera images from iPhone to my web server but some how the images change it's orientation when i download them from server.This only happens with camera images and not simple images uploaded to device from my machine.Plz help me.
i have use this condition to rotate image on server side:
//obj will be Orientation passed while uploading image on server its value will be either 1 or 3or 6 or 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;
}
But still i am getting the same problem
Here is my code for Upload image:
-(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 !!!");
}