问题
I need to upload an Image To webservice. Below is the code snippet i have return to upload an image. The image size is very big (around 6Mb). I am Uploading that Image in Background Thread using GCD.
if([VSCore ConnectedToInternet ])
{
bgTask = [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler: ^{
dispatch_async(dispatch_get_main_queue(), ^{
//[application endBackgroundTask:self->bgTask];
//self->bgTask = UIBackgroundTaskInvalid;
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[vrs write:data toURI:URI];
[[UIApplication sharedApplication]endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
//
}
-(BOOL)write:(NSData *)data toURI:(NSString *)URI
{
BOOL retVal = NO;
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:@"%d", [data length]];
NSRange range = [URI rangeOfString:@"http"];//Is http?
if(range.location != NSNotFound)
{
//Yes, http
NSMutableURLRequest *httpRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URI]];
[httpRequest setHTTPMethod:@"POST"];
[httpRequest setHTTPBody:data];
[httpRequest setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
[httpRequest setValue:requestDataLengthString forHTTPHeaderField:@"Content-Length"];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:httpRequest delegate:self];
[theConnection release];
[httpRequest release];
if (theConnection)
{
receivedData=[[NSMutableData data] retain];
retVal = YES;
}
else
{
NSError *error = [NSError alloc];
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
[error release];
retVal = NO;
}
}
return retVal;
}
now the problem i am facing is, if i try to upload the image in background Thread the request is not going to server ( I am checking the Log file on server). but if i upload Image in Main Thread the request is going to server (Just for testing purpose, I know that its not good idea to upload big images in main thread). So what am i doing wrong here ? is there any problem with Background Threading ? Plz Help me Out. Thanks in advance.
回答1:
Instead of doing it on a background thread. you could create a class that does your net connections like this.
you'll just need to add in the fields to post your image.
- (void)send: (NSString *)urlString {
self.receivedData = [[NSMutableData alloc] init];
NSURLRequest *request = [[NSURLRequest alloc]
initWithURL: [NSURL URLWithString:urlString]
cachePolicy: NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval: 20
];
NSURLConnection *connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self
startImmediately:YES];
if(!connection) {
NSLog(@"connection failed :(");
} else {
NSLog(@"connection succeeded :)");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
//NSLog(@"Received response: %@", response);
[receivedData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
//NSLog(@"Received %d bytes of data", [data length]);
[receivedData appendData:data];
//NSLog(@"Received data is now %d bytes", [receivedData length]);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Error receiving response: %@", error);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Once this method is invoked, "responseData" contains the complete result
//NSLog(@"Succeeded! Received %d bytes of data", [receivedData length]);
NSString *dataStr=[[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
NSLog(@"%@",dataStr);
}
you'll need this in the header:
@interface NetConnection : NSObject
{
NSMutableData *receivedData;
}
@property (nonatomic,retain) NSMutableData *receivedData;
@property (nonatomic,retain) NSString *callback;
来源:https://stackoverflow.com/questions/15896465/uploading-big-image-in-background-thread