2013-04-09 55 views
0

我需要上傳圖片到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; 

}

現在我面臨的問題是,如果我嘗試上傳背景圖片線程請求不會服務器(我正在檢查服務器上的日誌文件)。但如果我在主線程中上傳圖像,請求將發送到服務器(僅用於測試目的,我知道在主線程中上傳大圖像不是個好主意)。那麼我在這裏做錯了什麼?背景線程有什麼問題嗎? Plz幫助我。提前致謝。

回答

1

而不是在後臺線程上。你可以創建一個類來完成你的網絡連接。

你只需要添加字段來發布圖像。

- (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; 
+0

,如果我上傳的圖片一樣,你說,它不會引起我的UI,以阻止了一段時間? – RockandRoll 2013-04-09 11:47:00