サーバーに画像をアップロードしています。Synchronous リクエストを ASynchronous リクエストに変更する方法。画像のアップロード中にプログレスバーを使いたいので..
私のコードは.
+(NSDictionary *)UploadPhoto :(UIImage *)image{
NSString *DeviceId;
NSString *TokenValue=tokenTemp;
DeviceId = DevID;
NSString *Nonce=[self generateRandomString];
NSString *currentTimeStamp = [self GetTimeStamp];
NSString *Cipher=[NSString stringWithFormat:@"%@%@%@%@%@",mySecret,DeviceId,Nonce,currentTimeStamp,TokenValue];
Cipher=[Cipher URLDecodedString];
Cipher=[self GetCipher:Cipher];
UIImage *TempPicture=image;//[UIImage imageNamed:imageName];
NSData *PictureData=UIImageJPEGRepresentation(TempPicture, 1.0);
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@Messages.php",WebServiceUrl]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setTimeoutInterval:20.0f];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"multipart/form-data; boundary=*****" forHTTPHeaderField:@"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
NSString *stringBoundary = [NSString stringWithString:@"*****"];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"method\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%@",@"Upload"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"Type\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%@",@"0"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"Name\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%@",@"XYZ ****************"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"MessageImage\"; filename=\"c.png\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[NSData dataWithData:PictureData]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[request setHTTPBody: postBody];
[request setValue:Cipher forHTTPHeaderField:@"Cipher"];
[request setValue:DeviceId forHTTPHeaderField:@"Deviceid"];
[request setValue:Nonce forHTTPHeaderField:@"Nonce"];
[request setValue:currentTimeStamp forHTTPHeaderField:@"Timestamp"];
[request setValue:TokenValue forHTTPHeaderField:@"Token"];
[request setHTTPBody:postBody];
NSError *error;
NSHTTPURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
if (!data) {
NSLog(@"nil");
}
NSDictionary *temp= [response allHeaderFields];
NSString *temp2= [temp objectForKey:@"Responsecode"];
if(temp2!=nil && [temp2 length]>0)
{
}
else
{
}
return temp;
}
ありがとう..助けてください..
このコードを呼び出す方法
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
..