上传大背景线程中的图像。

我需要上载一个图像到web服务。 以下是我已经返回上传图片的代码片段。 图像尺寸非常大(约6Mb)。 我使用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; 

}

现在我面临的问题是,如果我尝试在后台上传图像线程请求不会去服务器(我正在检查服务器上的日志文件)。 但如果我在主线程中上传图像请求是要服务器(只是为了testing目的,我知道它不是主意上传大图像在主线程)。 那么我在这里做错了什么? 背景线程是否有问题? Plz帮助我。 提前致谢。

而不是在后台线程上做。 你可以创build一个类来完成你的networking连接。

你只需要添加字段发布您的图像。

 - (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); } 

你会在标题中需要这个:

 @interface NetConnection : NSObject { NSMutableData *receivedData; } @property (nonatomic,retain) NSMutableData *receivedData; @property (nonatomic,retain) NSString *callback;